1#include <string.h>
2#include <time.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <stdlib.h>
6
7#include "test_ccapi_util.h"
8
9
10// ---------------------------------------------------------------------------
11
12cc_int32 destroy_all_ccaches(cc_context_t context) {
13	cc_int32 err = ccNoError;
14	cc_ccache_t ccache = NULL;
15
16	while (!err) {
17		err = cc_context_open_default_ccache(context, &ccache);
18		if (!err) {
19			err = cc_ccache_destroy(ccache);
20		}
21	}
22	if (err == ccErrCCacheNotFound) {
23		err = ccNoError;
24	}
25	else {
26		log_error("cc_context_open_default_ccache or cc_ccache_destroy failed with %s (%d)", translate_ccapi_error(err), err);
27	}
28
29	return err;
30}
31
32
33// ---------------------------------------------------------------------------
34
35cc_int32 new_v5_creds_union (cc_credentials_union *out_union, const char *realm)
36{
37    cc_int32 err = ccNoError;
38    cc_credentials_union *cred_union = NULL;
39	cc_credentials_v5_t *v5creds = NULL;
40    static int num_runs = 1;
41	char *client = NULL;
42	char *server = NULL;
43
44    if (!out_union) { err = ccErrBadParam; }
45
46	if (!err) {
47		v5creds = malloc (sizeof (*v5creds));
48		if (!v5creds) {
49			err = ccErrNoMem;
50		}
51	}
52
53	if (!err) {
54		asprintf(&client, "client@%s", realm);
55		asprintf(&server, "host/%d%s@%s", num_runs++, realm, realm);
56		if (!client || !server) {
57			err = ccErrNoMem;
58		}
59	}
60
61	if (!err) {
62		v5creds->client = client;
63		v5creds->server = server;
64		v5creds->keyblock.type = 1;
65		v5creds->keyblock.length = 0;
66		v5creds->keyblock.data = NULL;
67		v5creds->authtime = time (NULL);
68		v5creds->starttime = time (NULL);
69		v5creds->endtime = time(NULL) + 1000;
70		v5creds->renew_till = time(NULL) + 10000;
71		v5creds->is_skey = 0;
72		v5creds->ticket_flags = TKT_FLG_FORWARDABLE | TKT_FLG_PROXIABLE | TKT_FLG_RENEWABLE | TKT_FLG_INITIAL;
73		v5creds->addresses = NULL;
74		v5creds->ticket.type = 0;
75		v5creds->ticket.length = 0;
76		v5creds->ticket.data = NULL;
77		v5creds->second_ticket.type = 0;
78		v5creds->second_ticket.length = 0;
79		v5creds->second_ticket.data = NULL;
80		v5creds->authdata = NULL;
81	}
82
83
84    if (!err) {
85        cred_union = malloc (sizeof (*cred_union));
86        if (cred_union) {
87			cred_union->version = cc_credentials_v5;
88			cred_union->credentials.credentials_v5 = v5creds;
89        } else {
90            err = ccErrNoMem;
91        }
92    }
93	if (!err) {
94		*out_union = *cred_union;
95		cred_union = NULL;
96	}
97
98    return err;
99}
100
101
102// ---------------------------------------------------------------------------
103
104void release_v5_creds_union(cc_credentials_union *creds_union) {
105	cc_credentials_v5_t *v5creds = NULL;
106
107	if (creds_union) {
108		if (creds_union->credentials.credentials_v5) {
109			v5creds = creds_union->credentials.credentials_v5;
110			if (v5creds->client) { free(v5creds->client); }
111			if (v5creds->server) { free(v5creds->server); }
112			if (v5creds->keyblock.data) { free(v5creds->keyblock.data); }
113			if (v5creds->ticket.data) { free(v5creds->ticket.data); }
114			if (v5creds->second_ticket.data) { free(v5creds->second_ticket.data); }
115			free(v5creds);
116		}
117		//free(creds_union);
118	}
119}
120
121
122// ---------------------------------------------------------------------------
123
124// return zero when both unions are considered equal, non-zero when not
125
126int compare_v5_creds_unions(const cc_credentials_union *a, const cc_credentials_union *b) {
127	int retval = -1;
128
129	if (a &&
130		b &&
131		(a->version == cc_credentials_v5) &&
132		(a->version == b->version) &&
133		(strcmp(a->credentials.credentials_v5->client, b->credentials.credentials_v5->client) == 0) &&
134		(strcmp(a->credentials.credentials_v5->server, b->credentials.credentials_v5->server) == 0))
135	{
136		retval = 0;
137	}
138
139	return retval;
140}
141