summaryrefslogtreecommitdiffstats
path: root/src/ccapi/lib/ccapi_v2.c
blob: 08100481b39c130fd7844cd30f55f325a44fbab7 (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
/*
 * $Header$
 *
 * Copyright 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 "cci_common.h"
#include "ccapi_string.h"
#include "ccapi_context.h"
#include "ccapi_ccache.h"
#include "ccapi_ccache_iterator.h"
#include "ccapi_credentials.h"
#include "ccapi_credentials_iterator.h"
#include <CredentialsCache2.h>

infoNC infoNC_initializer = { NULL, NULL, CC_CRED_UNKNOWN };

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

static cc_int32 cci_remap_version (cc_int32   in_v2_version,
                                   cc_uint32 *out_v3_version) 
{
    cc_result err = ccNoError;
    
    if (!out_v3_version) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        if (in_v2_version == CC_CRED_V4) {
            *out_v3_version = cc_credentials_v4;
            
        } else if (in_v2_version == CC_CRED_V5) { 
            *out_v3_version = cc_credentials_v5;
            
        } else {
            err = ccErrBadCredentialsVersion;
        }
    }
    
    return cci_check_error (err);
}    

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

static cc_result _cci_remap_error (cc_result   in_error, 
                                   const char *in_function, 
                                   const char *in_file, 
                                   int         in_line) 
{
    _cci_check_error (in_error, in_function, in_file, in_line);
    
    if (in_error >= CC_NOERROR && in_error <= CC_ERR_CRED_VERSION) {
        return in_error;
    }
    
    switch (in_error) {
        case ccNoError:
            return CC_NOERROR;
            
        case ccIteratorEnd:
            return CC_END;
            
        case ccErrBadParam:
        case ccErrContextNotFound:
        case ccErrInvalidContext:
        case ccErrInvalidCredentials:
        case ccErrInvalidCCacheIterator:
        case ccErrInvalidCredentialsIterator:
        case ccErrInvalidLock:
        case ccErrBadLockType:
            return CC_BAD_PARM;
            
        case ccErrNoMem:
            return CC_NOMEM;
            
        case ccErrInvalidCCache:
        case ccErrCCacheNotFound:
            return CC_NO_EXIST;
            
        case ccErrCredentialsNotFound:
            return CC_NOTFOUND;
            
        case ccErrBadName:
            return CC_BADNAME;
            
        case ccErrBadCredentialsVersion:
            return CC_ERR_CRED_VERSION;
            
        case ccErrBadAPIVersion:
            return CC_BAD_API_VERSION;
            
        case ccErrContextLocked:
        case ccErrContextUnlocked:
        case ccErrCCacheLocked:
        case ccErrCCacheUnlocked:
            return CC_LOCKED;
            
        case ccErrServerUnavailable:
        case ccErrServerInsecure:
        case ccErrServerCantBecomeUID:
        case ccErrBadInternalMessage:
        case ccErrClientNotFound:
            return CC_IO;
            
        case ccErrNotImplemented:
            return CC_NOT_SUPP;
            
        default:
            cci_debug_printf ("%s(): Unhandled error", __FUNCTION__);
            return CC_BAD_PARM;
    }
}
#define cci_remap_error(err) _cci_remap_error(err, __FUNCTION__, __FILE__, __LINE__)


#if TARGET_OS_MAC
#pragma mark -
#endif

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

cc_result cc_shutdown (apiCB **io_context) 
{
    cc_result err = ccNoError;
    
    if (!io_context) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_context_release (*io_context);
    }
    
    if (!err) {
        *io_context = NULL;
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_get_change_time (apiCB     *in_context,
                              cc_time_t *out_change_time) 
{
    cc_result err = ccNoError;
    
    if (!in_context     ) { err = cci_check_error (ccErrBadParam); }
    if (!out_change_time) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_context_get_change_time (in_context, out_change_time);
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_get_NC_info (apiCB    *in_context,
                          infoNC ***out_info)
{
    cc_result err = CC_NOERROR;
    infoNC **info = NULL;
    cc_uint64 count = 0; /* Preflight the size */
    cc_uint64 i;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!out_info  ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        ccache_cit *iterator = NULL;
        
        err = cc_seq_fetch_NCs_begin (in_context, &iterator);
        
        while (!err) {
            ccache_p *ccache = NULL;
            
            err = cc_seq_fetch_NCs_next (in_context, &ccache, iterator);
            
            if (!err) { count++; }
            
            if (ccache) { cc_close (in_context, &ccache); }
        }
        if (err == CC_END) { err = CC_NOERROR; }
        
        if (!err) {
            err = cc_seq_fetch_NCs_end (in_context, &iterator);
        }    
    }
    
    if (!err) {
        info = malloc (sizeof (*info) * (count + 1));
        if (info) { 
            for (i = 0; i < count + 1; i++) { info[i] = NULL; }
        } else { 
            err = cci_check_error (CC_NOMEM); 
        }
    }
    
    if (!err) {
        ccache_cit *iterator = NULL;
        
        err = cc_seq_fetch_NCs_begin (in_context, &iterator);
        
        for (i = 0; !err && i < count; i++) {
            ccache_p *ccache = NULL;
            
            err = cc_seq_fetch_NCs_next (in_context, &ccache, iterator);
            
            if (!err) {
                info[i] = malloc (sizeof (*info[i]));
                if (info[i]) { 
                    *info[i] = infoNC_initializer; 
                } else {
                    err = cci_check_error (CC_NOMEM); 
                }
            }
            
            if (!err) {
                err = cc_get_name (in_context, ccache, &info[i]->name);
            }
            
            if (!err) {
                err = cc_get_principal (in_context, ccache, &info[i]->principal);
            }
            
            if (!err) {
                err = cc_get_cred_version (in_context, ccache, &info[i]->vers);
            }
            
            if (ccache) { cc_close (in_context, &ccache); }
        }
        
        if (!err) {
            err = cc_seq_fetch_NCs_end (in_context, &iterator);
        }
    }
    
    if (!err) {
        *out_info = info;
        info = NULL;
    }
    
    if (info) { cc_free_NC_info (in_context, &info); }
    
    return cci_check_error (err);
}

#if TARGET_OS_MAC
#pragma mark -
#endif

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

cc_int32 cc_open (apiCB       *in_context,
                  const char  *in_name,
                  cc_int32     in_version,
                  cc_uint32    in_flags,
                  ccache_p   **out_ccache) 
{    
    cc_result err = ccNoError;
    cc_ccache_t ccache = NULL;
    cc_uint32 compat_version;
    cc_uint32 real_version;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!in_name   ) { err = cci_check_error (ccErrBadParam); }
    if (!out_ccache) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_remap_version (in_version, &compat_version);
    }

    if (!err) {
        err = ccapi_context_open_ccache (in_context, in_name, &ccache);
    }
    
    /* We must not allow a CCAPI v2 caller to open a v5-only ccache
     as a v4 ccache and vice versa. Allowing that would break 
     (valid) assumptions made by CCAPI v2 callers. */
    
    if (!err) {
        err = ccapi_ccache_get_credentials_version (ccache, &real_version);
    } 
    
    if (!err) {
        /* check the version and set up the ccache to use it */
        if (compat_version & real_version) {
            err = cci_ccache_set_compat_version (ccache, compat_version);
        } else {
            err = ccErrBadCredentialsVersion;
        }
    }
    
    if (!err) {
        *out_ccache = ccache;
        ccache = NULL;
    }
    
    if (ccache) { ccapi_ccache_release (ccache); }
    
    return cci_remap_error (err);
}    

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

cc_result cc_create (apiCB       *in_context,
                     const char  *in_name,
                     const char  *in_principal,
                     cc_int32     in_version,
                     cc_uint32    in_flags,
                     ccache_p   **out_ccache) 
{
    cc_result err = ccNoError;
    cc_ccache_t	ccache = NULL;
    cc_uint32 compat_version;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!in_name   ) { err = cci_check_error (ccErrBadParam); }
    if (!out_ccache) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_remap_version (in_version, &compat_version);
    }
    
    if (!err) {
        err = ccapi_context_create_ccache (in_context, in_name, compat_version,
                                           in_principal, &ccache);
    }
    
    if (!err) {
        err = cci_ccache_set_compat_version (ccache, compat_version);
    }
    
    if (!err) {
        *out_ccache = ccache;
        ccache = NULL;
    }
    
    if (ccache) { ccapi_ccache_release (ccache); }
    
    return cci_remap_error (err);
}

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

cc_result cc_close (apiCB     *in_context,
                    ccache_p **io_ccache) 
{
    cc_result err = ccNoError;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!io_ccache ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_ccache_release (*io_ccache);
    }
        
    if (!err) {
        *io_ccache = NULL;
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_destroy (apiCB     *in_context,
                      ccache_p **io_ccache) 
{
    cc_result err = ccNoError;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!io_ccache ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_ccache_destroy (*io_ccache);
    }
    
    if (!err) {
        *io_ccache = NULL;
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_get_name (apiCB     *in_context,
                       ccache_p  *in_ccache,
                       char     **out_name) 
{
    cc_result err = ccNoError;
    cc_string_t name = NULL;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!in_ccache ) { err = cci_check_error (ccErrBadParam); }
    if (!out_name  ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_ccache_get_name (in_ccache, &name);
    }
    
    if (!err) {
        char *string = strdup (name->data);
        if (string) { 
            *out_name = string;
        } else { 
            err = cci_check_error (ccErrNoMem); 
        }
    }
    
    if (name) { ccapi_string_release (name); }
    
    return cci_remap_error (err);    
}

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

cc_result cc_get_cred_version (apiCB    *in_context,
                               ccache_p *in_ccache,
                               cc_int32 *out_version) 
{
    cc_result err = ccNoError;
    cc_uint32 compat_version;
    
    if (!in_context ) { err = cci_check_error (ccErrBadParam); }
    if (!in_ccache  ) { err = cci_check_error (ccErrBadParam); }
    if (!out_version) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_ccache_get_compat_version (in_ccache, &compat_version);
    }
    
    if (!err) {
        if (compat_version == cc_credentials_v4) {
            *out_version = CC_CRED_V4;
            
        } else if (compat_version == cc_credentials_v5) { 
            *out_version = CC_CRED_V5;
            
        } else {
            err = ccErrBadCredentialsVersion;
        }
    }
    
    return cci_remap_error (err);    
}

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

cc_result cc_set_principal (apiCB    *in_context,
                            ccache_p *io_ccache,
                            cc_int32  in_version,
                            char     *in_principal) 
{
    cc_result err = ccNoError;
    cc_uint32 version;
    cc_uint32 compat_version;
    
    if (!in_context  ) { err = cci_check_error (ccErrBadParam); }
    if (!io_ccache   ) { err = cci_check_error (ccErrBadParam); }
    if (!in_principal) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_remap_version (in_version, &version);
    }

    if (!err) {
        err = cci_ccache_get_compat_version (io_ccache, &compat_version);
    }
    
    if (!err && version != compat_version) {
        err = cci_check_error (ccErrBadCredentialsVersion);
    }
    
    if (!err) {
        err = ccapi_ccache_set_principal (io_ccache, version, in_principal);
    }
    
    return cci_remap_error (err);    
}

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

cc_result cc_get_principal (apiCB      *in_context,
                            ccache_p   *in_ccache,
                            char      **out_principal) 
{
    cc_result err = ccNoError;
    cc_uint32 compat_version;
    cc_string_t principal = NULL;
    
    if (!in_context   ) { err = cci_check_error (ccErrBadParam); }
    if (!in_ccache    ) { err = cci_check_error (ccErrBadParam); }
    if (!out_principal) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_ccache_get_compat_version (in_ccache, &compat_version);
    }
    
    if (!err) {
        err = ccapi_ccache_get_principal (in_ccache, compat_version, &principal);
    }
    
    if (!err) {
        char *string = strdup (principal->data);
        if (string) { 
            *out_principal = string;
        } else { 
            err = cci_check_error (ccErrNoMem); 
        }
    }
    
    if (principal) { ccapi_string_release (principal); }
    
    return cci_remap_error (err);    
}

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

cc_result cc_store (apiCB      *in_context,
                    ccache_p   *io_ccache,
                    cred_union  in_credentials) 
{
    cc_result err = ccNoError;
    cc_credentials_union *creds_union = NULL;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!io_ccache ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_cred_union_to_credentials_union (&in_credentials,
                                                   &creds_union);
    }
    
    if (!err) {
        err = ccapi_ccache_store_credentials (io_ccache, creds_union);
    }
    
    if (creds_union) { cci_credentials_union_release (creds_union); }
    return cci_remap_error (err);
}

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

cc_result cc_remove_cred (apiCB      *in_context,
                          ccache_p   *in_ccache,
                          cred_union  in_credentials) 
{
    cc_result err = ccNoError;
    cc_credentials_iterator_t iterator = NULL;
    cc_uint32 found = 0;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!in_ccache ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_ccache_new_credentials_iterator (in_ccache, &iterator);
    }
    
    while (!err && !found) {
        cc_credentials_t creds = NULL;
        
        err = ccapi_credentials_iterator_next (iterator, &creds);
        
        if (!err) {
            err = cci_cred_union_compare_to_credentials_union (&in_credentials, 
                                                               creds->data,
                                                               &found);
        }
        
        if (!err && found) {
            err = ccapi_ccache_remove_credentials (in_ccache, creds);
        }
        
        ccapi_credentials_release (creds);
    }
    if (err == ccIteratorEnd) { err = cci_check_error (ccErrCredentialsNotFound); }
    
    return cci_remap_error (err);    
}

#if TARGET_OS_MAC
#pragma mark -
#endif

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

cc_result cc_seq_fetch_NCs_begin (apiCB       *in_context,
                                  ccache_cit **out_iterator) 
{
    cc_result err = ccNoError;
    cc_ccache_iterator_t iterator = NULL;
    
    if (!in_context  ) { err = cci_check_error (ccErrBadParam); }
    if (!out_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_context_new_ccache_iterator (in_context, &iterator);
    }
    
    if (!err) {
        *out_iterator = (ccache_cit *) iterator;
        iterator = NULL; /* take ownership */
    }
    
    if (iterator) { ccapi_ccache_iterator_release (iterator); }
    
    return cci_remap_error (err);
}

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

cc_result cc_seq_fetch_NCs_next (apiCB       *in_context,
                                 ccache_p   **out_ccache,
                                 ccache_cit  *in_iterator) 
{
    cc_result err = ccNoError;
    cc_ccache_iterator_t iterator = (cc_ccache_iterator_t) in_iterator;
    cc_ccache_t ccache = NULL;
    const char *saved_ccache_name;
    
    if (!in_context ) { err = cci_check_error (ccErrBadParam); }
    if (!out_ccache ) { err = cci_check_error (ccErrBadParam); }
    if (!in_iterator) { err = cci_check_error (ccErrBadParam); }
    
    /* CCache iterators need to return some ccaches twice (when v3 ccache has
     * two kinds of credentials). To do that, we return such ccaches twice
     * v4 first, then v5. */

    if (!err) {
        err = cci_ccache_iterator_get_saved_ccache_name (iterator,
                                                         &saved_ccache_name);
    }
    
    if (!err) {
        if (saved_ccache_name) {
            err = ccapi_context_open_ccache (in_context, saved_ccache_name, 
                                             &ccache);
            
            if (!err) {
                err = cci_ccache_set_compat_version (ccache, cc_credentials_v5);
            }
            
            if (!err) {
                err = cci_ccache_iterator_set_saved_ccache_name (iterator, NULL);
            }
            
        } else {
            cc_uint32 version = 0;
            
            err = ccapi_ccache_iterator_next (iterator, &ccache);
            
            if (!err) {
                err = ccapi_ccache_get_credentials_version (ccache, &version);
            }
            
            if (!err) {
                if (version == cc_credentials_v4_v5) {
                    cc_string_t name = NULL;
                    
                    err = cci_ccache_set_compat_version (ccache, cc_credentials_v4);
                    
                    if (!err) {                    
                        err = ccapi_ccache_get_name (ccache, &name);
                    } 
                
                    if (!err) {
                        err = cci_ccache_iterator_set_saved_ccache_name (iterator,  
                                                                         name->data);
                    }
                    
                    if (name) { ccapi_string_release (name); }
                    
                } else {
                    err = cci_ccache_set_compat_version (ccache, version);
                }
            }
        }
    }
    
    if (!err) {
        *out_ccache = ccache;
        ccache = NULL; /* take ownership */
    }
    
    if (ccache) { ccapi_ccache_release (ccache); }
        
    return cci_remap_error (err);
}

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

cc_result cc_seq_fetch_NCs_end (apiCB       *in_context,
                                ccache_cit **io_iterator) 
{
    cc_result err = ccNoError;
    cc_ccache_iterator_t iterator = (cc_ccache_iterator_t) *io_iterator;
    
    if (!in_context ) { err = cci_check_error (ccErrBadParam); }
    if (!io_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_ccache_iterator_release (iterator);
    }
    
    if (!err) {
        *io_iterator = NULL;
    }
    
    return cci_remap_error (err);
}

#if TARGET_OS_MAC
#pragma mark -
#endif

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

cc_result cc_seq_fetch_creds_begin (apiCB           *in_context,
                                    const ccache_p  *in_ccache,
                                    ccache_cit     **out_iterator) 
{
    cc_result err = ccNoError;
    cc_credentials_iterator_t iterator = NULL;
    cc_uint32 compat_version;
    
    if (!in_context  ) { err = cci_check_error (ccErrBadParam); }
    if (!in_ccache   ) { err = cci_check_error (ccErrBadParam); }
    if (!out_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_ccache_get_compat_version ((cc_ccache_t) in_ccache, 
                                             &compat_version);
    }
    
    if (!err) {
        err = ccapi_ccache_new_credentials_iterator ((cc_ccache_t) in_ccache, 
                                                     &iterator);
    }
    
    if (!err) {
        err = cci_credentials_iterator_set_compat_version (iterator, 
                                                           compat_version);
    }
    
    if (!err) {
        *out_iterator = (ccache_cit *) iterator;
        iterator = NULL; /* take ownership */
    }
    
    if (iterator) { ccapi_credentials_iterator_release (iterator); }
    
    return cci_remap_error (err);
}

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

cc_result cc_seq_fetch_creds_next (apiCB       *in_context,
                                   cred_union **out_creds,
                                   ccache_cit  *in_iterator) 
{
    cc_result err = ccNoError;
    cc_credentials_iterator_t iterator = (cc_credentials_iterator_t) in_iterator;
    cc_uint32 compat_version;
    
    if (!in_context ) { err = cci_check_error (ccErrBadParam); }
    if (!out_creds  ) { err = cci_check_error (ccErrBadParam); }
    if (!in_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_credentials_iterator_get_compat_version (iterator, 
                                                           &compat_version);
    }

    while (!err) {
        cc_credentials_t credentials = NULL;
        
        err = ccapi_credentials_iterator_next (iterator, &credentials);
        
        if (!err && (credentials->data->version & compat_version)) {
            /* got the next credentials for the correct version */
            err = cci_credentials_union_to_cred_union (credentials->data, 
                                                       out_creds);
            break;
        }
        
        if (credentials) { ccapi_credentials_release (credentials); }
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_seq_fetch_creds_end (apiCB       *in_context,
                                  ccache_cit **io_iterator) 
{
    cc_result err = ccNoError;
    cc_credentials_iterator_t iterator = (cc_credentials_iterator_t) *io_iterator;
    
    if (!in_context ) { err = cci_check_error (ccErrBadParam); }
    if (!io_iterator) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = ccapi_credentials_iterator_release (iterator);
    }
    
    if (!err) {
        *io_iterator = NULL;
    }
    
    return cci_remap_error (err);
}

#if TARGET_OS_MAC
#pragma mark -
#endif

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

cc_result cc_free_principal (apiCB  *in_context,
                             char  **io_principal) 
{
    cc_result err = ccNoError;
    
    if (!in_context  ) { err = cci_check_error (ccErrBadParam); }
    if (!io_principal) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        free (*io_principal);
        *io_principal = NULL;
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_free_name (apiCB  *in_context,
                        char  **io_name) 
{    
    cc_result err = ccNoError;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!io_name   ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        free (*io_name);
        *io_name = NULL;
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_free_creds (apiCB       *in_context,
                         cred_union **io_credentials) 
{
    cc_result err = ccNoError;
    
    if (!in_context    ) { err = cci_check_error (ccErrBadParam); }
    if (!io_credentials) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_cred_union_release (*io_credentials);
        if (!err) { *io_credentials = NULL; } 
    }
    
    return cci_remap_error (err);
}

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

cc_result cc_free_NC_info (apiCB    *in_context,
                           infoNC ***io_info) 
{    
    cc_result err = ccNoError;
    
    if (!in_context) { err = cci_check_error (ccErrBadParam); }
    if (!io_info   ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err && *io_info) {
        infoNC **data = *io_info;
        int i;
        
        for (i = 0; data[i] != NULL; i++) {
            cc_free_principal (in_context, &data[i]->principal);
            cc_free_name (in_context, &data[i]->name);
            free (data[i]);
        }
        free (data);
        
        *io_info = NULL;
    }
    
    return cci_remap_error (err);
}