summaryrefslogtreecommitdiffstats
path: root/src/kim/lib/kim_ccache.c
blob: 26e926d3b79dc21033672b2b3c00f3eb6187bc05 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
/*
 * Copyright 2005-2006 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include <krb5.h>
#include <Kerberos/CredentialsCache.h>
#include "kim_private.h"

struct kim_ccache_iterator_opaque {
    krb5_context context;
    krb5_cccol_cursor cursor;
    kim_boolean first;
};

struct kim_ccache_iterator_opaque kim_ccache_iterator_initializer = { NULL, NULL, 1 };

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_iterator_create (kim_ccache_iterator *out_ccache_iterator)
{
    kim_error err = kim_library_init ();
    kim_ccache_iterator ccache_iterator = NULL;
    
    if (!err && !out_ccache_iterator) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        ccache_iterator = malloc (sizeof (*ccache_iterator));
        if (ccache_iterator) { 
            *ccache_iterator = kim_ccache_iterator_initializer;
        } else {
            err = KIM_OUT_OF_MEMORY_ERR; 
        }
    }
    
    if (!err) {
        err = krb5_error (NULL, krb5_init_context (&ccache_iterator->context));
    }
    
    if (!err) {
        err = krb5_error (ccache_iterator->context,
                          krb5_cccol_cursor_new (ccache_iterator->context,
                                                 &ccache_iterator->cursor));
    }
    
    if (!err) {    
        *out_ccache_iterator = ccache_iterator;
        ccache_iterator = NULL;
    }
    
    kim_ccache_iterator_free (&ccache_iterator);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_iterator_next (kim_ccache_iterator  in_ccache_iterator,
                                    kim_ccache          *out_ccache)
{
    kim_error err = KIM_NO_ERROR;
    krb5_ccache ccache = NULL;
    
    if (!err && !in_ccache_iterator) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_ccache        ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = krb5_cccol_cursor_next (in_ccache_iterator->context, 
                                      in_ccache_iterator->cursor,
                                      &ccache);
        if (err == KRB5_CC_END) {
            ccache = NULL; /* out of ccaches */
            err = KIM_NO_ERROR;
        }        
    }
    
    if (!err && ccache && in_ccache_iterator->first) {
        krb5_principal principal = NULL;
        
        /* krb5 API is sneaky and returns a single empty ccache if the
         * cache collection is empty.  Check for it: */
        err = krb5_error (in_ccache_iterator->context,
                           krb5_cc_get_principal (in_ccache_iterator->context, 
                                                  ccache, 
                                                  &principal));
        
        if (err) {
            krb5_cc_close (in_ccache_iterator->context, ccache);
            ccache = NULL;
            err = KIM_NO_ERROR;
        }
    }
    
    if (!err) {
        in_ccache_iterator->first = 0;
        
        if (ccache) {
            err = kim_ccache_create_from_krb5_ccache (out_ccache,
                                                      in_ccache_iterator->context, 
                                                      ccache);
        } else {
            *out_ccache = NULL; /* no more ccaches */
        }        
    }
    
    if (ccache) { krb5_cc_close (in_ccache_iterator->context, ccache); }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

void kim_ccache_iterator_free (kim_ccache_iterator *io_ccache_iterator)
{
    if (io_ccache_iterator && *io_ccache_iterator) {
        if ((*io_ccache_iterator)->context) { 
            if ((*io_ccache_iterator)->cursor) {
                krb5_cccol_cursor_free ((*io_ccache_iterator)->context, 
                                        &(*io_ccache_iterator)->cursor);
            }
            krb5_free_context ((*io_ccache_iterator)->context); 
        }
        free (*io_ccache_iterator);
        *io_ccache_iterator = NULL;
    }
}

#pragma mark -

/* ------------------------------------------------------------------------ */

struct kim_ccache_opaque {
    krb5_context context;
    krb5_ccache ccache;
};

struct kim_ccache_opaque kim_ccache_initializer = { NULL, NULL };

/* ------------------------------------------------------------------------ */

static kim_error kim_ccache_create_resolve_name (kim_string *out_resolve_name,
                                                 kim_string  in_name,
                                                 kim_string  in_type)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !out_resolve_name) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_name         ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_type         ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_string_create_from_format (out_resolve_name, "%s:%s", 
                                             in_type, in_name);
    }
    
    return check_error (err);
}

#pragma mark -

/* ------------------------------------------------------------------------ */

static inline kim_error kim_ccache_allocate (kim_ccache *out_ccache)
{
    kim_error err = kim_library_init ();
    kim_ccache ccache = NULL;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        ccache = malloc (sizeof (*ccache));
        if (!ccache) { err = KIM_OUT_OF_MEMORY_ERR; }
    }
    
    if (!err) {
        *ccache = kim_ccache_initializer;
        *out_ccache = ccache;
        ccache = NULL;
    }
    
    kim_ccache_free (&ccache);
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_new (kim_ccache          *out_ccache,
                                 kim_identity         in_client_identity,
                                 kim_options          in_options)
{
    return check_error (kim_ccache_create_new_with_password (out_ccache,
                                                             in_client_identity,
                                                             in_options,
                                                             NULL));
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_new_with_password (kim_ccache   *out_ccache,
                                               kim_identity  in_client_identity,
                                               kim_options   in_options,
                                               kim_string    in_password)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    kim_identity client_identity = NULL;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_credential_create_new_with_password (&credential, 
                                                       in_client_identity, 
                                                       in_options,
                                                       in_password);
    }
    
    if (!err) {
        err = kim_credential_get_client_identity (credential, &client_identity);
    }
    
    if (!err) {
        err = kim_credential_store (credential, client_identity, out_ccache);
    }
    
    kim_identity_free (&client_identity);
    kim_credential_free (&credential);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_new_if_needed (kim_ccache   *out_ccache,
                                           kim_identity  in_client_identity,
                                           kim_options   in_options)
{
    return check_error (kim_ccache_create_new_if_needed_with_password (out_ccache,
                                                                       in_client_identity,
                                                                       in_options,
                                                                       NULL));
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_new_if_needed_with_password (kim_ccache   *out_ccache,
                                                         kim_identity  in_client_identity,
                                                         kim_options   in_options,
                                                         kim_string    in_password)
{
    kim_error err = KIM_NO_ERROR;
    kim_ccache ccache = NULL;
    
    if (!err && !out_ccache        ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_client_identity) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        kim_credential_state state;
        
        err = kim_ccache_create_from_client_identity (&ccache, 
                                                      in_client_identity);
        
        if (!err) {
            err = kim_ccache_get_state (ccache, &state);
        }
        
        if (!err && state != kim_credentials_state_valid) {
            if (state == kim_credentials_state_needs_validation) {
                err = kim_ccache_validate (ccache, in_options);
            } else {
                kim_ccache_free (&ccache);
                ccache = NULL;
            }
        }
        
        if (!ccache) {
            /* ccache does not already exist, create a new one */
            err = kim_ccache_create_new_with_password (&ccache, 
                                                       in_client_identity, 
                                                       in_options, 
                                                       in_password);
        }        
    }
    
    if (!err) {
        *out_ccache = ccache;
        ccache = NULL;
    }
    
    kim_ccache_free (&ccache);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_from_client_identity (kim_ccache   *out_ccache,
                                                  kim_identity  in_client_identity)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err && in_client_identity) {
        kim_ccache_iterator iterator = NULL;
        kim_boolean found = FALSE;

        err = kim_ccache_iterator_create (&iterator);
        
        while (!err && !found) {
            kim_ccache ccache = NULL;
            kim_identity identity = NULL;
            kim_comparison comparison;
            
            err = kim_ccache_iterator_next (iterator, &ccache);
            
            if (!err && !ccache) {
                kim_string string = NULL;
                
                err = kim_identity_get_display_string (in_client_identity, 
                                                       &string);
                
                if (!err) {
                    err = kim_error_set_message_for_code (KIM_NO_SUCH_PRINCIPAL_ERR, 
                                                          string);
                }
                
                kim_string_free (&string);
            }
            
            if (!err) {
                err = kim_ccache_get_client_identity (ccache, &identity);
            }
            
            if (!err) {
                err = kim_identity_compare (in_client_identity, identity, 
                                            &comparison);
            }
            
            if (!err && kim_comparison_is_equal_to (comparison)) {
                found = 1;
                *out_ccache = ccache;
                ccache = NULL;
            }
            
            kim_identity_free (&identity);
            kim_ccache_free (&ccache);
        }
        
        kim_ccache_iterator_free (&iterator);
        
    } else if (!err) {
        /* in_client_identity is NULL, get default ccache */
        err = kim_ccache_create_from_default (out_ccache);
    }
    
    return check_error (err);
}

#ifndef LEAN_CLIENT

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_from_keytab (kim_ccache    *out_ccache,
                                         kim_identity   in_identity,
                                         kim_options    in_options,
                                         kim_string     in_keytab)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    kim_identity client_identity = NULL;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_credential_create_from_keytab (&credential, in_identity, 
                                                 in_options, in_keytab);
    }
    
    if (!err) {
        err = kim_credential_get_client_identity (credential, &client_identity);
    }
    
    if (!err) {
        err = kim_credential_store (credential, client_identity, out_ccache);
    }
    
    kim_identity_free (&client_identity);
    kim_credential_free (&credential);
    
    return check_error (err);
}

#endif /* LEAN_CLIENT */

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_from_default (kim_ccache *out_ccache)
{
    kim_error err = KIM_NO_ERROR;
    kim_ccache ccache = NULL;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_allocate (&ccache);
    }
    
    if (!err) {
        err = krb5_error (NULL, krb5_init_context (&ccache->context));
    }
    
    if (!err) {
        err = krb5_error (ccache->context,
                          krb5_cc_default (ccache->context, &ccache->ccache));
    }
    
    if (!err) {
        *out_ccache = ccache;
        ccache = NULL;
    }
    
    kim_ccache_free (&ccache);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_from_display_name (kim_ccache  *out_ccache,
                                               kim_string   in_display_name)
{
    kim_error err = KIM_NO_ERROR;
    kim_ccache ccache = NULL;
    
    if (!err && !out_ccache     ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_display_name) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_allocate (&ccache);
    }
    
    if (!err) {
        err = krb5_error (NULL, krb5_init_context (&ccache->context));
    }
    
    if (!err) {
        err = krb5_error (ccache->context,
                          krb5_cc_resolve (ccache->context, in_display_name, 
                                           &ccache->ccache));
    }
    
    if (!err) {
        *out_ccache = ccache;
        ccache = NULL;
    }
    
    kim_ccache_free (&ccache);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_from_type_and_name (kim_ccache  *out_ccache,
                                                kim_string   in_type,
                                                kim_string   in_name)
{
    kim_error err = KIM_NO_ERROR;
    kim_string resolve_name = NULL;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_name   ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_type   ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_create_resolve_name (&resolve_name, in_name, in_type);
    }
    
    if (!err) {
        err = kim_ccache_create_from_display_name (out_ccache, resolve_name);
    }
    
    kim_string_free (&resolve_name);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_create_from_krb5_ccache (kim_ccache  *out_ccache,
                                              krb5_context   in_krb5_context,
                                              krb5_ccache    in_krb5_ccache)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !out_ccache     ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_krb5_ccache ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_krb5_context) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        kim_string type = krb5_cc_get_type (in_krb5_context, in_krb5_ccache);
        kim_string name = krb5_cc_get_name (in_krb5_context, in_krb5_ccache);
        
        err = kim_ccache_create_from_type_and_name (out_ccache, type, name);
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_copy (kim_ccache  *out_ccache,
                           kim_ccache   in_ccache)
{
    kim_error err = KIM_NO_ERROR;
    kim_string name = NULL;
    kim_string type = NULL;
    
    if (!err && !out_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_ccache ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_name (in_ccache, &name);
    }
    
    if (!err) {
        err = kim_ccache_get_type (in_ccache, &type);
    }
    
    if (!err) {
        err = kim_ccache_create_from_type_and_name (out_ccache, type, name);
    }
    
    kim_string_free (&name);
    kim_string_free (&type);
    
    return check_error (err);
}

#pragma mark -

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_compare (kim_ccache      in_ccache,
                              kim_ccache      in_compare_to_ccache,
                              kim_comparison *out_comparison)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_ccache           ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_compare_to_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_comparison      ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        const char *type            = krb5_cc_get_type (in_ccache->context, 
                                                        in_ccache->ccache);
        const char *compare_to_type = krb5_cc_get_type (in_compare_to_ccache->context, 
                                                        in_compare_to_ccache->ccache);
        const char *name            = krb5_cc_get_name (in_ccache->context, 
                                                        in_ccache->ccache);
        const char *compare_to_name = krb5_cc_get_name (in_compare_to_ccache->context, 
                                                        in_compare_to_ccache->ccache);
        
        *out_comparison = strcmp (type, compare_to_type);
        
        if (*out_comparison == 0) {
            *out_comparison = strcmp (name, compare_to_name);
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_krb5_ccache (kim_ccache  in_ccache,
                                      krb5_context  in_krb5_context,
                                      krb5_ccache  *out_krb5_ccache)
{
    kim_error err = KIM_NO_ERROR;
    kim_string resolve_name = NULL;
    
    if (!err && !in_ccache      ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_krb5_context) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_krb5_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_display_name (in_ccache, &resolve_name);
    }
    
    if (!err) {
        err = krb5_error (in_krb5_context,
                          krb5_cc_resolve (in_krb5_context, resolve_name, 
                                           out_krb5_ccache));
    }
    
    kim_string_free (&resolve_name);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_type (kim_ccache  in_ccache,
                               kim_string *out_type)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_type ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_string_copy (out_type, krb5_cc_get_type (in_ccache->context, 
                                                           in_ccache->ccache));
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_name (kim_ccache  in_ccache,
                               kim_string *out_name)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_name ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_string_copy (out_name, krb5_cc_get_name (in_ccache->context, 
                                                           in_ccache->ccache));
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_display_name (kim_ccache  in_ccache,
                                       kim_string *out_display_name)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_ccache       ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_display_name) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        kim_string type = krb5_cc_get_type (in_ccache->context, 
                                            in_ccache->ccache);
        kim_string name = krb5_cc_get_name (in_ccache->context, 
                                            in_ccache->ccache);
        
        err = kim_ccache_create_resolve_name (out_display_name, name, type);
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_client_identity (kim_ccache    in_ccache,
                                          kim_identity *out_client_identity)
{
    kim_error err = KIM_NO_ERROR;
    krb5_principal principal = NULL;
    
    if (!err && !in_ccache          ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_client_identity) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = krb5_error (in_ccache->context,
                          krb5_cc_get_principal (in_ccache->context, 
                                                 in_ccache->ccache, 
                                                 &principal));
    }
    
    if (!err) {
        err = kim_identity_create_from_krb5_principal (out_client_identity,
                                                       in_ccache->context, 
                                                       principal);
    }
    
    if (principal) { krb5_free_principal (in_ccache->context, principal); }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

static kim_error kim_ccache_get_dominant_credential (kim_ccache            in_ccache,
                                                     kim_credential_state *out_state,
                                                     kim_boolean          *out_is_tgt,
                                                     kim_credential       *out_credential)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential_iterator iterator = NULL;
    kim_boolean out_of_credentials = FALSE;
    kim_boolean found_valid_tgt = FALSE;
    kim_boolean dominant_is_tgt = FALSE;
    kim_credential_state dominant_state = kim_credentials_state_valid;
    kim_credential dominant_credential = NULL;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_credential_iterator_create (&iterator, in_ccache);
    }
    
    while (!err && !out_of_credentials && !found_valid_tgt) {
        kim_credential credential = NULL;
        
        err = kim_credential_iterator_next (iterator, &credential);
        
        if (!err && !credential) {
            out_of_credentials = TRUE;
            
        } else if (!err) {
            kim_credential_state state = kim_credentials_state_valid;
            kim_boolean is_tgt = FALSE;
            
            err = kim_credential_get_state (credential, &state);
            
            if (!err) {
                kim_identity service_identity = NULL;
                
                err = kim_credential_get_service_identity (credential, 
                                                           &service_identity);
                
                if (!err) {
                    err = kim_identity_is_tgt_service (service_identity, &is_tgt);
                }
                
                kim_identity_free (&service_identity);
            }
            
            if (!err) {
                /* There are three cases where we replace: 
                 * 1) We don't have a dominant yet
                 * 2) This is a tgt and dominant isn't
                 * 3) Both are tgts but this is valid and dominant isn't */
                
                if ((!dominant_credential)        /* 1 */ || 
                    (is_tgt && !dominant_is_tgt)  /* 2 */ ||
                    (is_tgt && dominant_is_tgt && /* 3 */ 
                     state == kim_credentials_state_valid &&
                     dominant_state != kim_credentials_state_valid)) {
                    /* replace */
                    kim_credential_free (&dominant_credential);
                    
                    dominant_credential = credential;
                    credential = NULL; /* take ownership */
                    
                    dominant_is_tgt = is_tgt;
                    dominant_state = state;
                }
                
                if (dominant_is_tgt && 
                    dominant_state == kim_credentials_state_valid) {
                    /* Since we will never replace a valid tgt, stop here */
                    found_valid_tgt = TRUE;
                }
            }
        }
        
        kim_credential_free (&credential);
    }
    
    if (!err && !dominant_credential) {
        kim_identity identity = NULL;
        kim_string identity_string = NULL;
        
        err = kim_ccache_get_client_identity (in_ccache, &identity);
        
        if (!err) {
            err = kim_identity_get_display_string (identity, 
                                                   &identity_string);
        }
        
        if (!err) {
            err = kim_error_set_message_for_code (KIM_NO_CREDENTIALS_ERR, 
                                                  identity_string);
        }    

        kim_string_free (&identity_string);
        kim_identity_free (&identity);
    }
        
    if (!err) {
        if (out_is_tgt) {
            *out_is_tgt = dominant_is_tgt;
        }
        
        if (out_state) {
            *out_state = dominant_state;
        }
         
        if (out_credential) {
            *out_credential = dominant_credential;
            dominant_credential = NULL;  /* take ownership */
        }
    }
    
    kim_credential_free (&dominant_credential);
    kim_credential_iterator_free (&iterator);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_valid_credential (kim_ccache      in_ccache,
                                           kim_credential *out_credential)
{
    kim_error err = KIM_NO_ERROR;
    kim_boolean is_tgt = FALSE;
    kim_credential_state state = kim_credentials_state_valid;
    kim_credential credential = NULL;
    
    if (!err && !in_ccache     ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_credential) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_dominant_credential (in_ccache, 
                                                  &state, &is_tgt, &credential);
    }
    
    if (!err && state != kim_credentials_state_valid) {
        kim_identity identity = NULL;
        kim_string identity_string = NULL;
        
        err = kim_ccache_get_client_identity (in_ccache, &identity);
        
        if (!err) {
            err = kim_identity_get_display_string (identity, 
                                                   &identity_string);
        }
        
        if (!err) {
            if (state == kim_credentials_state_expired) {
                err = kim_error_set_message_for_code (KIM_CREDENTIALS_EXPIRED_ERR, 
                                                  identity_string);
                
            } else if (state == kim_credentials_state_not_yet_valid ||
                       state == kim_credentials_state_needs_validation) {
                err = kim_error_set_message_for_code (KIM_NEEDS_VALIDATION_ERR, 
                                                      identity_string);
                
            } else if (state == kim_credentials_state_address_mismatch) {
                err = kim_error_set_message_for_code (KIM_BAD_IP_ADDRESS_ERR, 
                                                      identity_string);                
            } else {
                /* just default to this */
                err = kim_error_set_message_for_code (KIM_NEEDS_VALIDATION_ERR, 
                                                      identity_string);
            }
        }
        
        kim_string_free (&identity_string);
        kim_identity_free (&identity);
    }
    
    if (!err) {
        *out_credential = credential;
        credential = NULL;  /* take ownership */
    }
    
    kim_credential_free (&credential);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_state (kim_ccache            in_ccache,
                                kim_credential_state *out_state)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_state) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_dominant_credential (in_ccache, 
                                                  out_state, NULL, NULL);
    }
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_start_time (kim_ccache  in_ccache,
                                     kim_time   *out_start_time)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    
    if (!err && !in_ccache     ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_start_time) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_dominant_credential (in_ccache, NULL, NULL, 
                                                  &credential);
    }
    
    if (!err) {
        err = kim_credential_get_start_time (credential, out_start_time);
    }
    
    kim_credential_free (&credential);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_expiration_time (kim_ccache  in_ccache,
                                          kim_time   *out_expiration_time)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    
    if (!err && !in_ccache          ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_expiration_time) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_dominant_credential (in_ccache, NULL, NULL, 
                                                  &credential);
    }
    
    if (!err) {
        err = kim_credential_get_expiration_time (credential, 
                                                  out_expiration_time);
    }
    
    kim_credential_free (&credential);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_renewal_expiration_time (kim_ccache  in_ccache,
                                                  kim_time   *out_renewal_expiration_time)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    
    if (!err && !in_ccache                  ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_renewal_expiration_time) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_dominant_credential (in_ccache, NULL, NULL, 
                                                  &credential);
    }
    
    if (!err) {
        err = kim_credential_get_renewal_expiration_time (credential, 
                                                          out_renewal_expiration_time);
    }
    
    kim_credential_free (&credential);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_get_options (kim_ccache   in_ccache,
                                  kim_options *out_options)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    
    if (!err && !in_ccache  ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_options) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_dominant_credential (in_ccache, NULL, NULL, 
                                                  &credential);
    }
    
    if (!err) {
        err = kim_credential_get_options (credential, out_options);
    }
    
    kim_credential_free (&credential);
    
    return check_error (err);
}

#pragma mark -

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_set_default (kim_ccache io_ccache)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        char *environment_ccache_name = getenv ("KRB5CCNAME");
        
        if (environment_ccache_name) {
            kim_ccache environment_ccache = NULL;
            kim_comparison comparison;
            
            err = kim_ccache_create_from_display_name (&environment_ccache,
                                                       environment_ccache_name);
            
            if (!err) {
                err = kim_ccache_compare (io_ccache, 
                                          environment_ccache,
                                          &comparison);
            }
            
            if (!err && !kim_comparison_is_equal_to (comparison)) {
                krb5_principal client_principal = NULL;

                /* KRB5CCNAME is set and does not point to this ccache.  
                 * Move the creds and make this kim_ccache_t object refer to that ccache.  */
                
                err = krb5_error (io_ccache->context,
                                  krb5_cc_get_principal (io_ccache->context, 
                                                         io_ccache->ccache, 
                                                         &client_principal));
                
                if (!err) {
                    err = krb5_error (io_ccache->context,
                                      krb5_cc_initialize (environment_ccache->context, 
                                                          environment_ccache->ccache, 
                                                          client_principal));
                }
                
                if (!err) {
                    err = krb5_error (io_ccache->context,
                                      krb5_cc_copy_creds (io_ccache->context, 
                                                          io_ccache->ccache, 
                                                          environment_ccache->ccache));
                }
                
                if (client_principal) { krb5_free_principal (io_ccache->context, 
                                                             client_principal); }
                
                if (!err) {
                    kim_ccache_destroy (&io_ccache);
                    io_ccache = environment_ccache;
                    environment_ccache = NULL; /* take ownership */
                }
            }
            
            kim_ccache_free (&environment_ccache);
            
        } else {
#ifdef USE_CCAPI
            kim_string type = NULL;
            kim_string name = NULL;
            cc_context_t cc_context = NULL;
            cc_ccache_t cc_ccache = NULL;
            
            err = kim_ccache_get_type (io_ccache, &type);
            
            if (!err && strcmp (type, "API")) {
#endif
                kim_string display_name = NULL;
                /* Not a CCAPI ccache; can't set to default */
                
                err = kim_ccache_get_display_name (io_ccache, &display_name);
                
                if (!err) {
                    err = kim_error_set_message_for_code (KIM_CANT_BECOME_DEFAULT_ERR, 
                                                          display_name);
                }
                
                kim_string_free (&display_name);
#ifdef USE_CCAPI
            }
            
            if (!err) {
                err = kim_ccache_get_name (io_ccache, &name);
            }
            
            /* get a CCAPI ccache for this cache */
            if (!err) {
                err = cc_initialize (&cc_context, ccapi_version_4, NULL, NULL);
            }
            
            if (!err) {
                err = cc_context_open_ccache (cc_context, name, &cc_ccache);
            }
            
            if (!err) {
                err = cc_ccache_set_default (cc_ccache);
            }
            
            if (cc_context) { cc_context_release (cc_context); }
            if (cc_ccache ) { cc_ccache_release (cc_ccache); }
            kim_string_free (&name);
            kim_string_free (&type);
#endif
        }
    }
    
    return check_error (err);
}

#ifndef LEAN_CLIENT

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_verify (kim_ccache   in_ccache,
                             kim_identity in_service_identity,
                             kim_string   in_keytab,
                             kim_boolean  in_fail_if_no_service_key)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_valid_credential (in_ccache, &credential);
    }
    
    if (!err) {
        err = kim_credential_verify (credential, in_service_identity, 
                                     in_keytab, in_fail_if_no_service_key);
    }
    
    kim_credential_free (&credential);
    
    return check_error (err);
}

#endif /* LEAN_CLIENT */

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_renew (kim_ccache  in_ccache,
                            kim_options in_options)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    kim_identity client_identity = NULL;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_valid_credential (in_ccache, &credential);
    }
    
    if (!err) {
        err = kim_credential_renew (&credential, in_options);
    }
    
    if (!err) {
        err = kim_ccache_get_client_identity (in_ccache, &client_identity);
    }
    
    if (!err) {
        err = kim_credential_store (credential, client_identity, NULL);
    }
    
    kim_identity_free (&client_identity);
    kim_credential_free (&credential);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_validate (kim_ccache  in_ccache,
                               kim_options in_options)
{
    kim_error err = KIM_NO_ERROR;
    kim_credential credential = NULL;
    kim_identity client_identity = NULL;
    
    if (!err && !in_ccache) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_ccache_get_valid_credential (in_ccache, &credential);
    }
    
    if (!err) {
        err = kim_credential_validate (&credential, in_options);
    }
    
    if (!err) {
        err = kim_ccache_get_client_identity (in_ccache, &client_identity);
    }
    
    if (!err) {
        err = kim_credential_store (credential, client_identity, NULL);
    }
    
    kim_identity_free (&client_identity);
    kim_credential_free (&credential);
    
    return check_error (err);
}

#pragma mark -

/* ------------------------------------------------------------------------ */

kim_error kim_ccache_destroy (kim_ccache *io_ccache)
{
    kim_error err = KIM_NO_ERROR;
    
    if (io_ccache && *io_ccache) {
        err = krb5_error ((*io_ccache)->context,
                          krb5_cc_destroy ((*io_ccache)->context, 
                                           (*io_ccache)->ccache));
        
        if (!err) { 
            (*io_ccache)->ccache = NULL; 
            kim_ccache_free (io_ccache);
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

void kim_ccache_free (kim_ccache *io_ccache)
{
    if (io_ccache && *io_ccache) {
        if ((*io_ccache)->context) { 
            if ((*io_ccache)->ccache) {
                krb5_cc_close ((*io_ccache)->context, (*io_ccache)->ccache);
            }
            krb5_free_context ((*io_ccache)->context); 
        }
        free (*io_ccache);
        *io_ccache = NULL;
    }
}