xref: /freebsd/crypto/krb5/doc/doxy_examples/tkt_creds.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /** @example tkt_creds.c
2*7f2fe78bSCy Schubert  *
3*7f2fe78bSCy Schubert  *  Usage example for krb5_tkt_creds function family
4*7f2fe78bSCy Schubert  */
5*7f2fe78bSCy Schubert #include "krb5.h"
6*7f2fe78bSCy Schubert 
7*7f2fe78bSCy Schubert krb5_error_code
func(krb5_context context,krb5_flags options,krb5_ccache ccache,krb5_creds * in_creds,krb5_creds ** out_creds)8*7f2fe78bSCy Schubert func(krb5_context context, krb5_flags options,
9*7f2fe78bSCy Schubert      krb5_ccache ccache, krb5_creds *in_creds,
10*7f2fe78bSCy Schubert      krb5_creds **out_creds)
11*7f2fe78bSCy Schubert {
12*7f2fe78bSCy Schubert     krb5_error_code code = KRB5_OK;
13*7f2fe78bSCy Schubert     krb5_creds *ncreds = NULL;
14*7f2fe78bSCy Schubert     krb5_tkt_creds_context ctx = NULL;
15*7f2fe78bSCy Schubert 
16*7f2fe78bSCy Schubert     *out_creds = NULL;
17*7f2fe78bSCy Schubert 
18*7f2fe78bSCy Schubert     /* Allocate a container. */
19*7f2fe78bSCy Schubert     ncreds = k5alloc(sizeof(*ncreds), &code);
20*7f2fe78bSCy Schubert     if (ncreds == NULL)
21*7f2fe78bSCy Schubert         goto cleanup;
22*7f2fe78bSCy Schubert 
23*7f2fe78bSCy Schubert     /* Make and execute a krb5_tkt_creds context to get the credential. */
24*7f2fe78bSCy Schubert     code = krb5_tkt_creds_init(context, ccache, in_creds, options, &ctx);
25*7f2fe78bSCy Schubert     if (code != KRB5_OK)
26*7f2fe78bSCy Schubert         goto cleanup;
27*7f2fe78bSCy Schubert     code = krb5_tkt_creds_get(context, ctx);
28*7f2fe78bSCy Schubert     if (code != KRB5_OK)
29*7f2fe78bSCy Schubert         goto cleanup;
30*7f2fe78bSCy Schubert     code = krb5_tkt_creds_get_creds(context, ctx, ncreds);
31*7f2fe78bSCy Schubert     if (code != KRB5_OK)
32*7f2fe78bSCy Schubert         goto cleanup;
33*7f2fe78bSCy Schubert 
34*7f2fe78bSCy Schubert     *out_creds = ncreds;
35*7f2fe78bSCy Schubert     ncreds = NULL;
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert cleanup:
38*7f2fe78bSCy Schubert     krb5_free_creds(context, ncreds);
39*7f2fe78bSCy Schubert     krb5_tkt_creds_free(context, ctx);
40*7f2fe78bSCy Schubert     return code;
41*7f2fe78bSCy Schubert }
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert /* Allocate zeroed memory; set *code to 0 on success or ENOMEM on failure. */
44*7f2fe78bSCy Schubert static inline void *
k5alloc(size_t len,krb5_error_code * code)45*7f2fe78bSCy Schubert k5alloc(size_t len, krb5_error_code *code)
46*7f2fe78bSCy Schubert {
47*7f2fe78bSCy Schubert     void *ptr;
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert     /* Allocate at least one byte since zero-byte allocs may return NULL. */
50*7f2fe78bSCy Schubert     ptr = calloc((len > 0) ? len : 1, 1);
51*7f2fe78bSCy Schubert     *code = (ptr == NULL) ? ENOMEM : 0;
52*7f2fe78bSCy Schubert     return ptr;
53*7f2fe78bSCy Schubert }
54*7f2fe78bSCy Schubert 
55*7f2fe78bSCy Schubert 
56