xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/mech/copy_ccache.c (revision dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 #include "gssapiP_krb5.h"
4 
5 OM_uint32 KRB5_CALLCONV
6 gss_krb5int_copy_ccache(minor_status, cred_handle, out_ccache)
7      OM_uint32 *minor_status;
8      gss_cred_id_t cred_handle;
9      krb5_ccache out_ccache;
10 {
11    OM_uint32 stat;
12    krb5_gss_cred_id_t k5creds;
13    krb5_cc_cursor cursor;
14    krb5_creds creds;
15    krb5_error_code code;
16    krb5_context context;
17 
18    /* validate the cred handle */
19    stat = krb5_gss_validate_cred(minor_status, cred_handle);
20    if (stat)
21        return(stat);
22 
23    k5creds = (krb5_gss_cred_id_t) cred_handle;
24    code = k5_mutex_lock(&k5creds->lock);
25    if (code) {
26        *minor_status = code;
27        return GSS_S_FAILURE;
28    }
29    if (k5creds->usage == GSS_C_ACCEPT) {
30        k5_mutex_unlock(&k5creds->lock);
31        *minor_status = (OM_uint32) G_BAD_USAGE;
32        return(GSS_S_FAILURE);
33    }
34 
35    code = krb5_gss_init_context(&context);
36    if (code) {
37        k5_mutex_unlock(&k5creds->lock);
38        *minor_status = code;
39        return GSS_S_FAILURE;
40    }
41 
42    code = krb5_cc_start_seq_get(context, k5creds->ccache, &cursor);
43    if (code) {
44        k5_mutex_unlock(&k5creds->lock);
45        *minor_status = code;
46        krb5_free_context(context);
47        return(GSS_S_FAILURE);
48    }
49    while (!code && !krb5_cc_next_cred(context, k5creds->ccache, &cursor, &creds))
50        code = krb5_cc_store_cred(context, out_ccache, &creds);
51    krb5_cc_end_seq_get(context, k5creds->ccache, &cursor);
52    k5_mutex_unlock(&k5creds->lock);
53    krb5_free_context(context);
54    if (code) {
55        *minor_status = code;
56        return(GSS_S_FAILURE);
57    } else {
58        *minor_status = 0;
59        return(GSS_S_COMPLETE);
60    }
61 }
62