This repository has been archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gsi.c
1074 lines (904 loc) · 23.8 KB
/
gsi.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
/*
* University of Illinois/NCSA Open Source License
*
* Copyright © 2003-2012 NCSA. All rights reserved.
*
* Developed by:
*
* Storage Enabling Technologies (SET)
*
* Nation Center for Supercomputing Applications (NCSA)
*
* http://dims.ncsa.uiuc.edu/set
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the .Software.),
* to deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* + Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* + Neither the names of SET, NCSA
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED .AS IS., WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*/
/* System includes. */
#include <sys/socket.h>
#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>
#include <gssapi.h>
#include <netdb.h>
/* Local includes. */
#include "errcode.h"
#include "settings.h"
#include "radix.h"
#include "misc.h"
#include "gsi.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif /* DMALLOC */
#define SSL_TOK_LEN(x) (((((int)(x)[3]) << 8) & 0xFF00) | \
((((int)(x)[4]) << 0) & 0xFF) )
#define GSI_WRAP_LEN(x) (((((int)(x)[0]) << 24) & 0xFF000000) | \
((((int)(x)[1]) << 16) & 0x00FF0000) | \
((((int)(x)[2]) << 8) & 0x0000FF00) | \
((((int)(x)[3]) << 0) & 0x000000FF))
#define G_S_READ 0x00
#define G_S_GEN 0x01
#define G_S_WRITE 0x02
struct _gsi_handle {
gss_cred_id_t creds;
gss_ctx_id_t cntxt;
gss_name_t target;
int success;
/* Data channel operations. */
int upbsz; /* Unwrapped message buffer length. */
int state;
int done;
size_t cnt;
int len;
int eof;
char * buf;
char * ubuf; /* For writing, the unwrapped buffer. */
int ulen; /* For writing, the unwrapped length. */
int dcau; /* 0 no, 1 yes. */ /* Use s_dcau() for settings. */
int pbsz; /* Protection buffer size */
};
errcode_t
_g_compare_names(gh_t * gh);
errcode_t
_g_gen_token(gh_t * gh, int init);
errcode_t
_g_send_token(gh_t * gh, nh_t * nh);
errcode_t
_g_recv_token(gh_t * gh, nh_t * nh);
errcode_t
_g_acquire_cred(gss_cred_id_t * credp);
errcode_t
gsi_init()
{
OM_uint32 minor;
errcode_t ec = EC_SUCCESS;
gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
ec = _g_acquire_cred(&cred);
if (cred != GSS_C_NO_CREDENTIAL)
gss_release_cred(&minor, &cred);
return ec;
}
errcode_t
gsi_cc_init(gh_t ** ghp, char * host)
{
errcode_t ec = EC_SUCCESS;
OM_uint32 major;
OM_uint32 minor;
gss_buffer_desc input_name_buffer; /* Non pointer version of gss_buffer_t */
char * sname = NULL;
gh_t * gh = NULL;
gh = *ghp = (gh_t *) malloc(sizeof(gh_t));
memset(gh, 0, sizeof(gh_t));
gh->creds = GSS_C_NO_CREDENTIAL;
gh->cntxt = GSS_C_NO_CONTEXT;
gh->target = GSS_C_NO_NAME;
/*
* Acquire the user's credentials.
*/
ec = _g_acquire_cred(&gh->creds);
if (ec)
goto cleanup;
/*
* Construct the printable service name.
*/
sname = Sprintf(NULL, "host@%s", host);
/*
* Convert the name to the GSS internal format.
*/
input_name_buffer.value = sname;
input_name_buffer.length = strlen(sname) + 1;
major = gss_import_name(&minor,
&input_name_buffer,
GSS_C_NT_HOSTBASED_SERVICE,
&gh->target);
if (major != GSS_S_COMPLETE)
{
ec = ec_create(major,
minor,
"Failed to import service name");
goto cleanup;
}
cleanup:
if (ec != EC_SUCCESS)
{
gsi_destroy(gh);
*ghp = NULL;
}
FREE(sname);
return ec;
}
void
gsi_destroy(gh_t * gh)
{
OM_uint32 minor;
if (!gh)
return;
if (gh->cntxt != GSS_C_NO_CONTEXT)
gss_delete_sec_context(&minor, &gh->cntxt, GSS_C_NO_BUFFER);
if (gh->creds != GSS_C_NO_CREDENTIAL)
gss_release_cred(&minor, &gh->creds);
if (gh->target != GSS_C_NO_NAME)
gss_release_name(&minor, &gh->target);
FREE(gh->buf);
FREE(gh);
}
errcode_t
gsi_init_sec_context(gh_t * gh, char * input_token, char ** output_token)
{
errcode_t ec = EC_SUCCESS;
OM_uint32 major;
OM_uint32 minor;
gss_buffer_desc intok;
gss_buffer_desc outtok;
char * decoded_input = NULL;
int len = 0;
*output_token = NULL;
/* Radix decode the input_token. */
if (input_token)
{
len = strlen(input_token) - 2; /* Omit \r\n */
ec = radix_decode(input_token, &decoded_input, &len);
if (ec != EC_SUCCESS)
goto cleanup;
}
intok.value = decoded_input;
intok.length = len;
major = gss_init_sec_context(&minor,
gh->creds,
&gh->cntxt,
gh->target,
GSS_C_NO_OID,
GSS_C_MUTUAL_FLAG |
GSS_C_REPLAY_FLAG |
GSS_C_SEQUENCE_FLAG |
GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG |
GSS_C_DELEG_FLAG |
GSS_C_CONF_FLAG,
0,
GSS_C_NO_CHANNEL_BINDINGS,
&intok,
NULL,
&outtok,
NULL,
NULL);
FREE(decoded_input);
if (major != GSS_S_COMPLETE && major != GSS_S_CONTINUE_NEEDED)
{
ec = ec_create(major, minor, "Failed to init security context");
goto cleanup;
}
if (major == GSS_S_COMPLETE)
gh->success = 1;
/* Radix encode the output token. */
if (outtok.length)
{
len = outtok.length;
ec = radix_encode(outtok.value, output_token, &len);
gss_release_buffer(&minor, &outtok);
if (ec != EC_SUCCESS)
goto cleanup;
}
cleanup:
return ec;
}
errcode_t
gsi_cc_wrap(gh_t * gh, char * inmsg, char ** wmsg)
{
int len;
int conf_state;
OM_uint32 major;
OM_uint32 minor;
gss_buffer_desc inbuf;
gss_buffer_desc outbuf;
errcode_t ec = EC_SUCCESS;
/*
* Despite what the documents say, apparently you can not gss_wrap
* until the security context is complete.
*/
if (!gh || !gh->success)
{
*wmsg = Strdup(inmsg);
return ec;
}
inbuf.value = inmsg;
inbuf.length = strlen(inmsg);
major = gss_wrap (&minor,
gh->cntxt,
s_prot() == 3,
GSS_C_QOP_DEFAULT,
&inbuf,
&conf_state,
&outbuf);
if (major != GSS_S_COMPLETE)
return ec_create(major, minor, "gss_wrap() failed");
len = outbuf.length;
ec = radix_encode(outbuf.value, wmsg, &len);
gss_release_buffer(&minor, &outbuf);
if (ec != EC_SUCCESS)
return ec;
if (conf_state)
*wmsg = Sprintf(NULL, "ENC %s", *wmsg);
else
*wmsg = Sprintf(NULL, "MIC %s", *wmsg);
return ec;
}
errcode_t
gsi_cc_unwrap(gh_t * gh, char * inmsg, char ** umsg, int len)
{
char * decoded_msg;
OM_uint32 major;
OM_uint32 minor;
gss_buffer_desc inbuf;
gss_buffer_desc outbuf;
errcode_t ec = EC_SUCCESS;
if (!gh || !gh->success)
{
*umsg = Strdup(inmsg);
return ec;
}
/* Radix decode. */
ec = radix_decode(inmsg, &decoded_msg, &len);
if (ec != EC_SUCCESS)
return ec;
/* Gssapi unwrap. */
inbuf.value = decoded_msg;
inbuf.length = len;
major = gss_unwrap(&minor,
gh->cntxt,
&inbuf,
&outbuf,
NULL,
NULL);
FREE(decoded_msg);
if (major != GSS_S_COMPLETE)
return ec_create(major, minor, "Error while unwrapping message");
*umsg = Strndup(outbuf.value, outbuf.length);
gss_release_buffer(&minor, &outbuf);
return ec;
}
errcode_t
gsi_dc_auth(gh_t ** ghp, nh_t * nh, int pbsz, int dcau, int accept, int * done)
{
gh_t * gh = *ghp;
errcode_t ec = EC_SUCCESS;
if (!gh)
{
gh = *ghp = (gh_t *) malloc(sizeof(gh_t));
memset(gh, 0, sizeof(gh_t));
gh->creds = GSS_C_NO_CREDENTIAL;
gh->cntxt = GSS_C_NO_CONTEXT;
gh->target = GSS_C_NO_NAME;
gh->dcau = dcau;
gh->pbsz = pbsz;
if (accept)
gh->state = G_S_READ;
else
gh->state = G_S_GEN;
}
*done = 1;
if (!gh->dcau)
return ec;
*done = 0;
switch (gh->state)
{
case G_S_READ: /* read */
ec = _g_recv_token(gh, nh);
break;
case G_S_GEN: /* Gen */
ec = _g_gen_token(gh, !accept);
if (ec || !gh->cnt)
break;
/* Fall through */
case G_S_WRITE: /* write */
ec = _g_send_token(gh, nh);
break;
}
if (gh->done && !gh->cnt)
*done = 1;
return ec;
}
errcode_t
gsi_dc_fl_read(gh_t * gh, nh_t * nh)
{
errcode_t ec = EC_SUCCESS;
size_t count = 0;
if (gh->eof)
return ec;
if (gh->dcau &&
s_prot() &&
gh->cnt > 4 &&
(SSL_TOK_LEN(gh->buf) + 5) <= gh->cnt)
{
return ec;
}
if ((gh->len - gh->cnt) < s_blocksize())
{
gh->len += s_blocksize();
gh->buf = (char *) realloc(gh->buf, gh->len);
}
count = gh->len-gh->cnt;
ec = net_read(nh,
gh->buf + gh->cnt,
&count,
&gh->eof);
if (count > 0)
gh->cnt += count;
return ec;
}
errcode_t
gsi_dc_fl_write(gh_t * gh, nh_t * nh)
{
errcode_t ec = EC_SUCCESS;
size_t count = 0;
int off = 0;
gss_buffer_desc uwbuf;
gss_buffer_desc wbuf;
OM_uint32 major;
OM_uint32 minor;
int cstate = 0;
do {
if (!gh->buf && gh->ubuf)
{
uwbuf.value = gh->ubuf;
uwbuf.length = gh->ulen;
/* Can only wrap up to upbsz bytes. */
if (gh->ulen > gh->upbsz)
uwbuf.length = gh->upbsz;
major = gss_wrap(&minor,
gh->cntxt,
s_prot() == 3,
GSS_C_QOP_DEFAULT,
&uwbuf,
&cstate,
&wbuf);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to wrap buffer.\n");
memmove(gh->ubuf, gh->ubuf+uwbuf.length, gh->ulen-uwbuf.length);
gh->ulen -= uwbuf.length;
if (gh->ulen == 0)
{
FREE(gh->ubuf);
gh->ubuf = 0;
}
gh->buf = wbuf.value;
gh->len = wbuf.length;
gh->cnt = wbuf.length;
}
if (gh->buf)
{
off = gh->len - gh->cnt;
count = gh->cnt;
ec = net_write_nb(nh,
gh->buf+off,
&count);
if (ec == EC_SUCCESS)
{
gh->cnt = count;
if (count == 0)
{
gh->cnt = 0;
gh->len = 0;
FREE(gh->buf);
gh->buf = NULL;
}
}
}
} while ((gh->ubuf || gh->buf) && gh->eof && ec == EC_SUCCESS);
return ec;
}
errcode_t
gsi_dc_read(gh_t * gh,
nh_t * nh,
char ** buf,
size_t * len,
int * eof)
{
int cstate = 0;
gss_qop_t qstate = GSS_C_QOP_DEFAULT;
OM_uint32 major;
OM_uint32 minor;
gss_buffer_desc wbuf;
gss_buffer_desc uwbuf;
if (gh->dcau && s_prot() && gh->cnt)
{
wbuf.value = gh->buf;
wbuf.length = SSL_TOK_LEN(gh->buf) + 5;
major = gss_unwrap(&minor,
gh->cntxt,
&wbuf,
&uwbuf,
&cstate,
&qstate);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to unwrap buffer");
*buf = (char *)uwbuf.value;
*len = uwbuf.length;
gh->cnt -= SSL_TOK_LEN(gh->buf) + 5;
memmove(gh->buf,
gh->buf + SSL_TOK_LEN(gh->buf) + 5,
gh->cnt);
*eof = gh->eof;
return EC_SUCCESS;
}
*buf = gh->buf;
*len = gh->cnt;
*eof = gh->eof;
gh->buf = NULL;
gh->cnt = 0;
gh->len = 0;
return EC_SUCCESS;
}
errcode_t
gsi_dc_write(gh_t * gh,
nh_t * nh,
char * buf,
size_t len,
int eof)
{
errcode_t ec = EC_SUCCESS;
gh->eof = eof;
if (gh->dcau && s_prot() && len)
{
gh->ubuf = buf;
gh->ulen = len;
}
else
{
gh->buf = buf;
gh->cnt = len;
gh->len = len;
}
if (eof)
ec = gsi_dc_fl_write(gh, nh);
return EC_SUCCESS;
}
int
gsi_dc_ready(gh_t * gh, nh_t * nh, int read)
{
if (read)
{
if (gh->eof)
return 1;
if (gh->cnt && (!gh->dcau || !s_prot()))
return 1;
if (gh->dcau && gh->cnt > 4 && (SSL_TOK_LEN(gh->buf)+5) <= gh->cnt)
return 1;
}
if (!read && !gh->buf && !gh->ubuf && !gh->eof)
return 1;
return 0;
}
/* Finds max encoded msg size for given buffer length. */
errcode_t
gsi_pbsz_maxpmsg(gh_t * gh, int umsglen, int * pmsglen)
{
errcode_t ec = EC_SUCCESS;
int tryulen = 0;
int tryelen = 0;
int stryulen = 0;
int stryelen = 0;
tryelen = umsglen + (5 * 1024);
while ((ec = gsi_pbsz_maxumsg(gh, tryelen, &tryulen)) == EC_SUCCESS)
{
if (stryulen)
{
if (stryulen > umsglen && tryulen < umsglen)
break;
if (stryulen < umsglen && tryulen > umsglen)
break;
}
stryulen = tryulen;
stryelen = tryelen;
if (tryulen > umsglen)
tryelen -= (5 * 1024);
else
tryelen += (5 * 1024);
}
*pmsglen = stryelen;
return ec;
}
/* Finds max unencoded msg size */
errcode_t
gsi_pbsz_maxumsg(gh_t * gh, int pmsglen, int * umsglen)
{
OM_uint32 size_req = pmsglen;
OM_uint32 max_size;
OM_uint32 major;
OM_uint32 minor;
major = gss_wrap_size_limit(&minor,
gh->cntxt,
s_prot() == 3,
GSS_C_QOP_DEFAULT,
size_req,
&max_size);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"gss_wrap_size_limit() failed.");
*umsglen = max_size;
return EC_SUCCESS;
}
errcode_t
_g_compare_names(gh_t * gh)
{
int equal = 0;
errcode_t ec = EC_SUCCESS;
gss_name_t tname;
gss_buffer_desc tbuf;
OM_uint32 major;
OM_uint32 minor;
char * name = NULL;
switch(s_dcau())
{
case 0: /* None. Will never happen. */
assert(0);
case 1: /* Self. */
major = gss_inquire_cred(&minor,
gh->creds,
&tname,
NULL,
NULL,
NULL);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to inquire credential.");
break;
case 2: /* Subject. */
tbuf.value = s_dcau_subject();
tbuf.length = strlen(tbuf.value);
major = gss_import_name(&minor,
&tbuf,
GSS_C_NO_OID,
&tname);
Free(tbuf.value);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to import name.");
break;
}
major = gss_compare_name(&minor,
gh->target,
tname,
&equal);
if (major != GSS_S_COMPLETE)
ec = ec_create(major,
minor,
"Failed to compare names.");
gss_release_name(&minor, &tname);
if (ec)
return ec;
if (equal)
return ec;
major = gss_display_name(&minor,
gh->target,
&tbuf,
NULL);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to get display name.");
name = Strndup(tbuf.value, tbuf.length);
ec = ec_create(EC_GSI_SUCCESS,
EC_GSI_SUCCESS,
"The remote service's identity is wrong: %s",
name);
Free(name);
gss_release_buffer(&minor, &tbuf);
return ec;
}
errcode_t
_g_gen_token(gh_t * gh, int init)
{
errcode_t ec = EC_SUCCESS;
gss_buffer_desc itoken = GSS_C_EMPTY_BUFFER;
gss_buffer_desc otoken = GSS_C_EMPTY_BUFFER;
gss_buffer_desc tbuf = GSS_C_EMPTY_BUFFER;
OM_uint32 major;
OM_uint32 minor;
OM_uint32 flags = 0;
if (gh->creds == GSS_C_NO_CREDENTIAL)
{
ec = _g_acquire_cred(&gh->creds);
if (ec)
return ec;
if (init)
{
switch(s_dcau())
{
case 0: /* None. Will never happen. */
assert(0);
case 1: /* Self. */
major = gss_inquire_cred(&minor,
gh->creds,
&gh->target,
NULL,
NULL,
NULL);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to inquire credential.");
break;
case 2: /* Subject. */
tbuf.value = s_dcau_subject();
tbuf.length = strlen(tbuf.value);
major = gss_import_name(&minor,
&tbuf,
GSS_C_NO_OID,
&gh->target);
Free(tbuf.value);
if (major != GSS_S_COMPLETE)
return ec_create(major,
minor,
"Failed to import name.");
break;
}
}
}
switch (s_prot())
{
case 0:
flags = GSS_C_MUTUAL_FLAG;
break;
case 1:
flags = GSS_C_MUTUAL_FLAG|GSS_C_INTEG_FLAG|GSS_C_GLOBUS_SSL_COMPATIBLE;
break;
case 2:
flags = GSS_C_MUTUAL_FLAG|GSS_C_CONF_FLAG|GSS_C_GLOBUS_SSL_COMPATIBLE;
break;
case 3:
flags = GSS_C_MUTUAL_FLAG|
GSS_C_CONF_FLAG|
GSS_C_INTEG_FLAG|
GSS_C_GLOBUS_SSL_COMPATIBLE;
break;
}
itoken.value = gh->buf;
itoken.length = gh->cnt;
if (init)
{
major = gss_init_sec_context(&minor,
gh->creds,
&gh->cntxt,
gh->target,
GSS_C_NO_OID,
flags,
0,
GSS_C_NO_CHANNEL_BINDINGS,
&itoken,
NULL,
&otoken,
NULL,
NULL);
if (major != GSS_S_COMPLETE && major != GSS_S_CONTINUE_NEEDED)
return ec_create(major,
minor,
"Failed to init security context.");
} else
{
major = gss_accept_sec_context(&minor,
&gh->cntxt,
gh->creds,
&itoken,
GSS_C_NO_CHANNEL_BINDINGS,
&gh->target,
NULL,
&otoken,
&flags,
NULL,
NULL);
if (major != GSS_S_COMPLETE && major != GSS_S_CONTINUE_NEEDED)
return ec_create(major,
minor,
"Failed to accept security context.");
}
if (major == GSS_S_COMPLETE)
{
/* Compare targets. */
if (!init)
ec = _g_compare_names(gh);
if (ec)
return ec;
/* Get the buffer limit */
ec = gsi_pbsz_maxumsg(gh, gh->pbsz, &gh->upbsz);
if (ec)
return ec;
if (gh->upbsz <= 0)
return ec_create(EC_GSI_SUCCESS,
EC_GSI_SUCCESS,
"Given PBSZ size (%d) is too small.\n",
gh->pbsz);
gh->done = 1;
}
FREE(gh->buf);
gh->cnt = gh->len = 0;
gh->state = G_S_READ; /* read */
if (otoken.length)
{
gh->buf = otoken.value;
gh->len = gh->cnt = otoken.length;
gh->state = G_S_WRITE; /* Write */
}
return ec;
}
errcode_t
_g_send_token(gh_t * gh, nh_t * nh)
{
errcode_t ec = EC_SUCCESS;
size_t off = gh->len - gh->cnt;
ec = net_write_nb(nh, gh->buf + off, &gh->cnt);
if (!gh->cnt)
{
FREE(gh->buf);
gh->buf = NULL;
gh->cnt = gh->len = 0;
gh->state = G_S_READ; /* read */
}
return ec;
}
errcode_t
_g_recv_token(gh_t * gh, nh_t * nh)
{
errcode_t ec = EC_SUCCESS;
size_t count = 0;
int eof = 0;
if (gh->len < 5)
{
gh->len = 5;
gh->buf = (char *) realloc(gh->buf, gh->len);
}
if (gh->cnt < 5)
{
count = 5 - gh->cnt;
ec = net_read(nh, gh->buf + gh->cnt, &count, &eof);
if (ec)
return ec;
gh->cnt += count;
}
if (gh->cnt < 5)
return ec;
/* SSL Header:
* byte 0: 20-26
* byte 1: 3
* byte 2: 0|1
* byte 3: MSB length
* byte 4: LSB length
*/
count = SSL_TOK_LEN(gh->buf) + 5;
if (gh->len < count)
{
gh->len = count;
gh->buf = (char *) realloc(gh->buf, gh->len);
}
count -= gh->cnt;
ec = net_read(nh, gh->buf + gh->cnt, &count, &eof);
if (!ec)
gh->cnt += count;
if (!ec && gh->cnt == gh->len)
gh->state = G_S_GEN; /* Gen */
return ec;
}
errcode_t
_g_acquire_cred(gss_cred_id_t * credp)
{
OM_uint32 major;
OM_uint32 minor;
static gss_buffer_desc exported_creds = {0, NULL};
#if defined MSSFTP && defined MSSFTP_GSI_SERVICE
gss_buffer_desc input_name_buffer;
gss_name_t target;
#endif /* MSSFTP && MSSFTP_GSI_SERVICE */