17c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bdstevel@tonic-gate
3505d05cgtb#include "k5-int.h"
47c478bdstevel@tonic-gate
5505d05cgtbkrb5_error_code KRB5_CALLCONV
6505d05cgtbkrb5_cc_copy_creds(krb5_context context, krb5_ccache incc, krb5_ccache outcc)
77c478bdstevel@tonic-gate{
87c478bdstevel@tonic-gate    krb5_error_code code;
97c478bdstevel@tonic-gate    krb5_flags flags;
10505d05cgtb    krb5_cc_cursor cur = 0;
117c478bdstevel@tonic-gate    krb5_creds creds;
127c478bdstevel@tonic-gate
137c478bdstevel@tonic-gate    flags = 0;				/* turns off OPENCLOSE mode */
14505d05cgtb    if ((code = krb5_cc_set_flags(context, incc, flags)))
157c478bdstevel@tonic-gate	return(code);
167c478bdstevel@tonic-gate    /* the code for this will open the file for reading only, which
177c478bdstevel@tonic-gate       is not what I had in mind.  So I won't turn off OPENCLOSE
187c478bdstevel@tonic-gate       for the output ccache */
197c478bdstevel@tonic-gate#if 0
207c478bdstevel@tonic-gate    if ((code = krb5_cc_set_flags(context, outcc, flags)))
217c478bdstevel@tonic-gate	return(code);
227c478bdstevel@tonic-gate#endif
237c478bdstevel@tonic-gate
24505d05cgtb    if ((code = krb5_cc_start_seq_get(context, incc, &cur)))
257c478bdstevel@tonic-gate	goto cleanup;
267c478bdstevel@tonic-gate
27505d05cgtb    while (!(code = krb5_cc_next_cred(context, incc, &cur, &creds))) {
287c478bdstevel@tonic-gate	code = krb5_cc_store_cred(context, outcc, &creds);
297c478bdstevel@tonic-gate	krb5_free_cred_contents(context, &creds);
307c478bdstevel@tonic-gate	if (code)
317c478bdstevel@tonic-gate	    goto cleanup;
327c478bdstevel@tonic-gate    }
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate    if (code != KRB5_CC_END)
357c478bdstevel@tonic-gate	goto cleanup;
367c478bdstevel@tonic-gate
37505d05cgtb    code = krb5_cc_end_seq_get(context, incc, &cur);
38505d05cgtb    cur = 0;
39505d05cgtb    if (code)
40505d05cgtb        goto cleanup;
41505d05cgtb
427c478bdstevel@tonic-gate    code = 0;
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gatecleanup:
457c478bdstevel@tonic-gate    flags = KRB5_TC_OPENCLOSE;
467c478bdstevel@tonic-gate
47505d05cgtb    /* If set then we are in an error pathway */
48505d05cgtb    if (cur)
49505d05cgtb      krb5_cc_end_seq_get(context, incc, &cur);
50505d05cgtb
517c478bdstevel@tonic-gate    if (code)
52505d05cgtb	krb5_cc_set_flags(context, incc, flags);
537c478bdstevel@tonic-gate    else
547c478bdstevel@tonic-gate	code = krb5_cc_set_flags(context, incc, flags);
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate#if 0
577c478bdstevel@tonic-gate    if (code)
587c478bdstevel@tonic-gate	krb5_cc_set_flags(context, outcc, flags);
597c478bdstevel@tonic-gate    else
607c478bdstevel@tonic-gate	code = krb5_cc_set_flags(context, outcc, flags);
617c478bdstevel@tonic-gate#endif
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate    return(code);
647c478bdstevel@tonic-gate}
65