1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/krb5/os/t_trace.c - Test harness for trace.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2012 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert #include "k5-int.h"
34*7f2fe78bSCy Schubert #include "port-sockets.h"
35*7f2fe78bSCy Schubert #include "os-proto.h"
36*7f2fe78bSCy Schubert #include <com_err.h>
37*7f2fe78bSCy Schubert
38*7f2fe78bSCy Schubert const char *prog;
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert static void
kfatal(krb5_error_code err)41*7f2fe78bSCy Schubert kfatal (krb5_error_code err)
42*7f2fe78bSCy Schubert {
43*7f2fe78bSCy Schubert com_err (prog, err, "- exiting");
44*7f2fe78bSCy Schubert exit (1);
45*7f2fe78bSCy Schubert }
46*7f2fe78bSCy Schubert
47*7f2fe78bSCy Schubert int
main(int argc,char * argv[])48*7f2fe78bSCy Schubert main (int argc, char *argv[])
49*7f2fe78bSCy Schubert {
50*7f2fe78bSCy Schubert char *p;
51*7f2fe78bSCy Schubert krb5_context ctx;
52*7f2fe78bSCy Schubert krb5_error_code err;
53*7f2fe78bSCy Schubert int i = -1;
54*7f2fe78bSCy Schubert long ln = -2;
55*7f2fe78bSCy Schubert size_t s = 0;
56*7f2fe78bSCy Schubert char *str = "example.data";
57*7f2fe78bSCy Schubert krb5_octet *oct = (krb5_octet *) str;
58*7f2fe78bSCy Schubert unsigned int oct_length = strlen(str);
59*7f2fe78bSCy Schubert struct remote_address ra;
60*7f2fe78bSCy Schubert struct sockaddr_in *addr_in;
61*7f2fe78bSCy Schubert krb5_data data;
62*7f2fe78bSCy Schubert struct krb5_key_st key;
63*7f2fe78bSCy Schubert krb5_checksum checksum;
64*7f2fe78bSCy Schubert krb5_principal_data principal_data, principal_data2;
65*7f2fe78bSCy Schubert krb5_principal princ = &principal_data;
66*7f2fe78bSCy Schubert krb5_pa_data padata, padata2, **padatap;
67*7f2fe78bSCy Schubert krb5_enctype enctypes[4] = {
68*7f2fe78bSCy Schubert ENCTYPE_DES3_CBC_SHA, ENCTYPE_ARCFOUR_HMAC_EXP, ENCTYPE_UNKNOWN,
69*7f2fe78bSCy Schubert ENCTYPE_NULL};
70*7f2fe78bSCy Schubert krb5_ccache ccache;
71*7f2fe78bSCy Schubert krb5_keytab keytab;
72*7f2fe78bSCy Schubert krb5_creds creds;
73*7f2fe78bSCy Schubert
74*7f2fe78bSCy Schubert p = strrchr (argv[0], '/');
75*7f2fe78bSCy Schubert if (p)
76*7f2fe78bSCy Schubert prog = p+1;
77*7f2fe78bSCy Schubert else
78*7f2fe78bSCy Schubert prog = argv[0];
79*7f2fe78bSCy Schubert
80*7f2fe78bSCy Schubert if (argc != 1) {
81*7f2fe78bSCy Schubert fprintf (stderr, "%s: usage: %s\n", prog, prog);
82*7f2fe78bSCy Schubert return 1;
83*7f2fe78bSCy Schubert }
84*7f2fe78bSCy Schubert
85*7f2fe78bSCy Schubert err = krb5_init_context (&ctx);
86*7f2fe78bSCy Schubert if (err)
87*7f2fe78bSCy Schubert kfatal (err);
88*7f2fe78bSCy Schubert
89*7f2fe78bSCy Schubert krb5int_trace(NULL, NULL);
90*7f2fe78bSCy Schubert TRACE(ctx, "simple format");
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert TRACE(ctx, "int, in decimal: {int}", i);
93*7f2fe78bSCy Schubert TRACE(ctx, "long, in decimal: {long}", ln);
94*7f2fe78bSCy Schubert
95*7f2fe78bSCy Schubert TRACE(ctx, "const char *, display as C string: {str}", str);
96*7f2fe78bSCy Schubert s = strlen(str);
97*7f2fe78bSCy Schubert TRACE(ctx, "size_t and const char *, as a counted string: {lenstr}",
98*7f2fe78bSCy Schubert s, str);
99*7f2fe78bSCy Schubert TRACE(ctx, "size_t and const char *, as a counted string: {lenstr}",
100*7f2fe78bSCy Schubert 1, NULL);
101*7f2fe78bSCy Schubert TRACE(ctx, "size_t and const char *, as hex bytes: {hexlenstr}",
102*7f2fe78bSCy Schubert s, str);
103*7f2fe78bSCy Schubert TRACE(ctx, "size_t and const char *, as hex bytes: {hexlenstr}",
104*7f2fe78bSCy Schubert 1, NULL);
105*7f2fe78bSCy Schubert TRACE(ctx, "size_t and const char *, as four-character hex hash: "
106*7f2fe78bSCy Schubert "{hashlenstr}", s, str);
107*7f2fe78bSCy Schubert TRACE(ctx, "size_t and const char *, as four-character hex hash: "
108*7f2fe78bSCy Schubert "{hashlenstr}", 1, NULL);
109*7f2fe78bSCy Schubert
110*7f2fe78bSCy Schubert ra.transport = TCP;
111*7f2fe78bSCy Schubert addr_in = (struct sockaddr_in *)&ra.saddr;
112*7f2fe78bSCy Schubert addr_in->sin_family = AF_INET;
113*7f2fe78bSCy Schubert addr_in->sin_addr.s_addr = INADDR_ANY;
114*7f2fe78bSCy Schubert addr_in->sin_port = htons(88);
115*7f2fe78bSCy Schubert ra.len = sizeof(struct sockaddr_in);
116*7f2fe78bSCy Schubert ra.family = AF_INET;
117*7f2fe78bSCy Schubert TRACE(ctx, "struct remote_address *, show socket type, address, port: "
118*7f2fe78bSCy Schubert "{raddr}", &ra);
119*7f2fe78bSCy Schubert ra.transport = UDP;
120*7f2fe78bSCy Schubert TRACE(ctx, "struct remote_address *, show socket type, address, port: "
121*7f2fe78bSCy Schubert "{raddr}", &ra);
122*7f2fe78bSCy Schubert ra.transport = 1234;
123*7f2fe78bSCy Schubert addr_in->sin_family = AF_UNSPEC;
124*7f2fe78bSCy Schubert ra.family = AF_UNSPEC;
125*7f2fe78bSCy Schubert TRACE(ctx, "struct remote_address *, show socket type, address, port: "
126*7f2fe78bSCy Schubert "{raddr}", &ra);
127*7f2fe78bSCy Schubert ra.family = 5678;
128*7f2fe78bSCy Schubert TRACE(ctx, "struct remote_address *, show socket type, address, port: "
129*7f2fe78bSCy Schubert "{raddr}", &ra);
130*7f2fe78bSCy Schubert
131*7f2fe78bSCy Schubert data.magic = 0;
132*7f2fe78bSCy Schubert data.length = strlen(str);
133*7f2fe78bSCy Schubert data.data = str;
134*7f2fe78bSCy Schubert TRACE(ctx, "krb5_data *, display as counted string: {data}", &data);
135*7f2fe78bSCy Schubert TRACE(ctx, "krb5_data *, display as counted string: {data}", NULL);
136*7f2fe78bSCy Schubert TRACE(ctx, "krb5_data *, display as hex bytes: {hexdata}", &data);
137*7f2fe78bSCy Schubert TRACE(ctx, "krb5_data *, display as hex bytes: {hexdata}", NULL);
138*7f2fe78bSCy Schubert
139*7f2fe78bSCy Schubert TRACE(ctx, "int, display as number/errorstring: {errno}", ENOENT);
140*7f2fe78bSCy Schubert TRACE(ctx, "krb5_error_code, display as number/errorstring: {kerr}", 0);
141*7f2fe78bSCy Schubert
142*7f2fe78bSCy Schubert key.keyblock.magic = 0;
143*7f2fe78bSCy Schubert key.keyblock.enctype = ENCTYPE_UNKNOWN;
144*7f2fe78bSCy Schubert key.keyblock.length = strlen(str);
145*7f2fe78bSCy Schubert key.keyblock.contents = (krb5_octet *)str;
146*7f2fe78bSCy Schubert key.refcount = 0;
147*7f2fe78bSCy Schubert key.derived = NULL;
148*7f2fe78bSCy Schubert key.cache = NULL;
149*7f2fe78bSCy Schubert TRACE(ctx, "const krb5_keyblock *, display enctype and hash of key: "
150*7f2fe78bSCy Schubert "{keyblock}", &key.keyblock);
151*7f2fe78bSCy Schubert TRACE(ctx, "const krb5_keyblock *, display enctype and hash of key: "
152*7f2fe78bSCy Schubert "{keyblock}", NULL);
153*7f2fe78bSCy Schubert TRACE(ctx, "krb5_key, display enctype and hash of key: {key}", &key);
154*7f2fe78bSCy Schubert TRACE(ctx, "krb5_key, display enctype and hash of key: {key}", NULL);
155*7f2fe78bSCy Schubert
156*7f2fe78bSCy Schubert checksum.magic = 0;
157*7f2fe78bSCy Schubert checksum.checksum_type = -1;
158*7f2fe78bSCy Schubert checksum.length = oct_length;
159*7f2fe78bSCy Schubert checksum.contents = oct;
160*7f2fe78bSCy Schubert TRACE(ctx, "const krb5_checksum *, display cksumtype and hex checksum: "
161*7f2fe78bSCy Schubert "{cksum}", &checksum);
162*7f2fe78bSCy Schubert
163*7f2fe78bSCy Schubert principal_data.magic = 0;
164*7f2fe78bSCy Schubert principal_data.realm.magic = 0;
165*7f2fe78bSCy Schubert principal_data.realm.data = "ATHENA.MIT.EDU";
166*7f2fe78bSCy Schubert principal_data.realm.length = strlen(principal_data.realm.data);
167*7f2fe78bSCy Schubert principal_data.data = &data;
168*7f2fe78bSCy Schubert principal_data.length = 0;
169*7f2fe78bSCy Schubert principal_data.type = KRB5_NT_UNKNOWN;
170*7f2fe78bSCy Schubert TRACE(ctx, "krb5_principal, unparse and display: {princ}", princ);
171*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_UNKNOWN);
172*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_PRINCIPAL);
173*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SRV_INST);
174*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SRV_HST);
175*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SRV_XHST);
176*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_UID);
177*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_X500_PRINCIPAL);
178*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SMTP_NAME);
179*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}",
180*7f2fe78bSCy Schubert KRB5_NT_ENTERPRISE_PRINCIPAL);
181*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_WELLKNOWN);
182*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_MS_PRINCIPAL);
183*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}",
184*7f2fe78bSCy Schubert KRB5_NT_MS_PRINCIPAL_AND_ID);
185*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}",
186*7f2fe78bSCy Schubert KRB5_NT_ENT_PRINCIPAL_AND_ID);
187*7f2fe78bSCy Schubert TRACE(ctx, "int, krb5_principal type: {ptype}", -1);
188*7f2fe78bSCy Schubert
189*7f2fe78bSCy Schubert padatap = malloc(sizeof(krb5_pa_data *) * 3);
190*7f2fe78bSCy Schubert padatap[0] = &padata;
191*7f2fe78bSCy Schubert padatap[1] = &padata2;
192*7f2fe78bSCy Schubert padatap[2] = NULL;
193*7f2fe78bSCy Schubert padata.magic = 0;
194*7f2fe78bSCy Schubert padata.pa_type = KRB5_PADATA_NONE;
195*7f2fe78bSCy Schubert padata.length = oct_length;
196*7f2fe78bSCy Schubert padata.contents = oct;
197*7f2fe78bSCy Schubert padata2 = padata;
198*7f2fe78bSCy Schubert padata.pa_type = KRB5_PADATA_PW_SALT;
199*7f2fe78bSCy Schubert TRACE(ctx, "krb5_pa_data **, display list of padata type numbers: "
200*7f2fe78bSCy Schubert "{patypes}", padatap);
201*7f2fe78bSCy Schubert TRACE(ctx, "krb5_pa_data **, display list of padata type numbers: "
202*7f2fe78bSCy Schubert "{patypes}", NULL);
203*7f2fe78bSCy Schubert free(padatap);
204*7f2fe78bSCy Schubert padatap = NULL;
205*7f2fe78bSCy Schubert
206*7f2fe78bSCy Schubert TRACE(ctx, "krb5_enctype, display shortest name of enctype: {etype}",
207*7f2fe78bSCy Schubert ENCTYPE_AES128_CTS_HMAC_SHA1_96);
208*7f2fe78bSCy Schubert TRACE(ctx, "krb5_enctype *, display list of enctypes: {etypes}", enctypes);
209*7f2fe78bSCy Schubert TRACE(ctx, "krb5_enctype *, display list of enctypes: {etypes}", NULL);
210*7f2fe78bSCy Schubert
211*7f2fe78bSCy Schubert err = krb5_cc_resolve(ctx, "FILE:/path/to/ccache", &ccache);
212*7f2fe78bSCy Schubert TRACE(ctx, "krb5_ccache, display type:name: {ccache}", ccache);
213*7f2fe78bSCy Schubert krb5_cc_close(ctx, ccache);
214*7f2fe78bSCy Schubert
215*7f2fe78bSCy Schubert err = krb5_kt_resolve(ctx, "FILE:/etc/krb5.keytab", &keytab);
216*7f2fe78bSCy Schubert TRACE(ctx, "krb5_keytab, display name: {keytab}", keytab);
217*7f2fe78bSCy Schubert krb5_kt_close(ctx, keytab);
218*7f2fe78bSCy Schubert
219*7f2fe78bSCy Schubert creds.magic = 0;
220*7f2fe78bSCy Schubert creds.client = &principal_data;
221*7f2fe78bSCy Schubert memcpy(&principal_data2, &principal_data, sizeof(principal_data));
222*7f2fe78bSCy Schubert principal_data2.realm.data = "ZEUS.MIT.EDU";
223*7f2fe78bSCy Schubert principal_data2.realm.length = strlen(principal_data2.realm.data);
224*7f2fe78bSCy Schubert creds.server = &principal_data2;
225*7f2fe78bSCy Schubert memcpy(&creds.keyblock, &key.keyblock, sizeof(creds.keyblock));
226*7f2fe78bSCy Schubert creds.times.authtime = 0;
227*7f2fe78bSCy Schubert creds.times.starttime = 1;
228*7f2fe78bSCy Schubert creds.times.endtime = 2;
229*7f2fe78bSCy Schubert creds.times.renew_till = 3;
230*7f2fe78bSCy Schubert creds.is_skey = FALSE;
231*7f2fe78bSCy Schubert creds.ticket_flags = 0;
232*7f2fe78bSCy Schubert creds.addresses = NULL;
233*7f2fe78bSCy Schubert creds.ticket.magic = 0;
234*7f2fe78bSCy Schubert creds.ticket.length = strlen(str);
235*7f2fe78bSCy Schubert creds.ticket.data = str;
236*7f2fe78bSCy Schubert creds.second_ticket.magic = 0;
237*7f2fe78bSCy Schubert creds.second_ticket.length = strlen(str);
238*7f2fe78bSCy Schubert creds.second_ticket.data = str;
239*7f2fe78bSCy Schubert creds.authdata = NULL;
240*7f2fe78bSCy Schubert TRACE(ctx, "krb5_creds *, display clientprinc -> serverprinc: {creds}",
241*7f2fe78bSCy Schubert &creds);
242*7f2fe78bSCy Schubert
243*7f2fe78bSCy Schubert krb5_free_context(ctx);
244*7f2fe78bSCy Schubert return 0;
245*7f2fe78bSCy Schubert }
246