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