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