c90c202c4066a36cc1da93020ce47efaafe0a792
[krb5.git] / src / lib / krb5 / os / t_trace.c
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 <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include "port-sockets.h"
37 #include <com_err.h>
38
39 #define TEST
40 #include "k5-int.h"
41 #include "cm.h"
42
43 const char *prog;
44
45 static void
46 kfatal (krb5_error_code err)
47 {
48     com_err (prog, err, "- exiting");
49     exit (1);
50 }
51
52 int
53 main (int argc, char *argv[])
54 {
55     char *p;
56     krb5_context ctx;
57     krb5_error_code err;
58     int i = -1;
59     long ln = -2;
60     size_t s = 0;
61     char *str = "example.data";
62     krb5_octet *oct = (krb5_octet *) str;
63     unsigned int oct_length = strlen(str);
64     struct conn_state conn;
65     struct sockaddr_in *addr_in;
66     krb5_data data;
67     struct krb5_key_st key;
68     krb5_checksum checksum;
69     krb5_principal_data principal_data, principal_data2;
70     krb5_principal princ = &principal_data;
71     krb5_pa_data padata, padata2, **padatap;
72     krb5_enctype enctypes[4] = {
73         ENCTYPE_DES3_CBC_SHA, ENCTYPE_ARCFOUR_HMAC_EXP, ENCTYPE_UNKNOWN,
74         ENCTYPE_NULL};
75     krb5_ccache ccache;
76     krb5_keytab keytab;
77     krb5_creds creds;
78
79     p = strrchr (argv[0], '/');
80     if (p)
81         prog = p+1;
82     else
83         prog = argv[0];
84
85     if (argc != 1) {
86         fprintf (stderr, "%s: usage: %s\n", prog, prog);
87         return 1;
88     }
89
90     err = krb5_init_context (&ctx);
91     if (err)
92         kfatal (err);
93
94     krb5int_trace(NULL, NULL);
95     TRACE(ctx, "simple format");
96
97     TRACE(ctx, "int, in decimal: {int}", i);
98     TRACE(ctx, "long, in decimal: {long}", ln);
99
100     TRACE(ctx, "const char *, display as C string: {str}", str);
101     s = strlen(str);
102     TRACE(ctx, "size_t and const char *, as a counted string: {lenstr}",
103           s, str);
104     TRACE(ctx, "size_t and const char *, as a counted string: {lenstr}",
105           1, NULL);
106     TRACE(ctx, "size_t and const char *, as hex bytes: {hexlenstr}",
107           s, str);
108     TRACE(ctx, "size_t and const char *, as hex bytes: {hexlenstr}",
109           1, NULL);
110     TRACE(ctx, "size_t and const char *, as four-character hex hash: "
111           "{hashlenstr}", s, str);
112     TRACE(ctx, "size_t and const char *, as four-character hex hash: "
113           "{hashlenstr}", 1, NULL);
114
115     conn.socktype = SOCK_STREAM;
116     addr_in = (struct sockaddr_in *) &conn.addr;
117     addr_in->sin_family = AF_INET;
118     addr_in->sin_addr.s_addr = INADDR_ANY;
119     addr_in->sin_port = htons(88);
120     TRACE(ctx, "struct conn_state *, show socket type, address, port: "
121           "{connstate}", &conn);
122     conn.socktype = SOCK_DGRAM;
123     TRACE(ctx, "struct conn_state *, show socket type, address, port: "
124           "{connstate}", &conn);
125     conn.socktype = SOCK_RDM;
126     addr_in->sin_family = AF_UNSPEC;
127     TRACE(ctx, "struct conn_state *, show socket type, address, port: "
128           "{connstate}", &conn);
129     conn.family = AF_UNSPEC;
130     TRACE(ctx, "struct conn_state *, show socket type, address, port: "
131           "{connstate}", &conn);
132
133     data.magic = 0;
134     data.length = strlen(str);
135     data.data = str;
136     TRACE(ctx, "krb5_data *, display as counted string: {data}", &data);
137     TRACE(ctx, "krb5_data *, display as counted string: {data}", NULL);
138     TRACE(ctx, "krb5_data *, display as hex bytes: {hexdata}", &data);
139     TRACE(ctx, "krb5_data *, display as hex bytes: {hexdata}", NULL);
140
141     TRACE(ctx, "int, display as number/errorstring: {errno}", 0);
142     TRACE(ctx, "int, display as number/errorstring: {errno}", 1);
143     TRACE(ctx, "krb5_error_code, display as number/errorstring: {kerr}", 0);
144
145     key.keyblock.magic = 0;
146     key.keyblock.enctype = ENCTYPE_UNKNOWN;
147     key.keyblock.length = strlen(str);
148     key.keyblock.contents = (krb5_octet *)str;
149     key.refcount = 0;
150     key.derived = NULL;
151     key.cache = NULL;
152     TRACE(ctx, "const krb5_keyblock *, display enctype and hash of key: "
153           "{keyblock}", &key.keyblock);
154     TRACE(ctx, "const krb5_keyblock *, display enctype and hash of key: "
155           "{keyblock}", NULL);
156     TRACE(ctx, "krb5_key, display enctype and hash of key: {key}", &key);
157     TRACE(ctx, "krb5_key, display enctype and hash of key: {key}", NULL);
158
159     checksum.magic = 0;
160     checksum.checksum_type = -1;
161     checksum.length = oct_length;
162     checksum.contents = oct;
163     TRACE(ctx, "const krb5_checksum *, display cksumtype and hex checksum: "
164           "{cksum}", &checksum);
165
166     principal_data.magic = 0;
167     principal_data.realm.magic = 0;
168     principal_data.realm.data = "ATHENA.MIT.EDU";
169     principal_data.realm.length = strlen(principal_data.realm.data);
170     principal_data.data = &data;
171     principal_data.length = 0;
172     principal_data.type = KRB5_NT_UNKNOWN;
173     TRACE(ctx, "krb5_principal, unparse and display: {princ}", princ);
174     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_UNKNOWN);
175     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_PRINCIPAL);
176     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SRV_INST);
177     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SRV_HST);
178     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SRV_XHST);
179     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_UID);
180     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_X500_PRINCIPAL);
181     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_SMTP_NAME);
182     TRACE(ctx, "int, krb5_principal type: {ptype}",
183           KRB5_NT_ENTERPRISE_PRINCIPAL);
184     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_WELLKNOWN);
185     TRACE(ctx, "int, krb5_principal type: {ptype}", KRB5_NT_MS_PRINCIPAL);
186     TRACE(ctx, "int, krb5_principal type: {ptype}",
187           KRB5_NT_MS_PRINCIPAL_AND_ID);
188     TRACE(ctx, "int, krb5_principal type: {ptype}",
189           KRB5_NT_ENT_PRINCIPAL_AND_ID);
190     TRACE(ctx, "int, krb5_principal type: {ptype}", -1);
191
192     padatap = (krb5_pa_data **) malloc(sizeof(krb5_pa_data *)*2);
193     padatap[0] = &padata;
194     memcpy(&padata2, &padata, sizeof(padata));
195     padatap[1] = &padata2;
196     padatap[2] = NULL;
197     padata.magic = 0;
198     padata.pa_type = KRB5_PADATA_NONE;
199     padata.length = oct_length;
200     padata.contents = oct;
201     TRACE(ctx, "krb5_pa_data **, display list of padata type numbers: "
202           "{patypes}", padatap);
203     TRACE(ctx, "krb5_pa_data **, display list of padata type numbers: "
204           "{patypes}", NULL);
205     free(padatap);
206     padatap = NULL;
207
208     TRACE(ctx, "krb5_enctype, display shortest name of enctype: {etype}",
209           ENCTYPE_DES_CBC_CRC);
210     TRACE(ctx, "krb5_enctype *, display list of enctypes: {etypes}", enctypes);
211     TRACE(ctx, "krb5_enctype *, display list of enctypes: {etypes}", NULL);
212
213     err = krb5_cc_default(ctx, &ccache);
214     TRACE(ctx, "krb5_ccache, display type:name: {ccache}", ccache);
215     krb5_cc_close(ctx, ccache);
216
217     err = krb5_kt_default(ctx, &keytab);
218     TRACE(ctx, "krb5_keytab, display name: {keytab}", keytab);
219     krb5_kt_close(ctx, keytab);
220
221     creds.magic = 0;
222     creds.client = &principal_data;
223     memcpy(&principal_data2, &principal_data, sizeof(principal_data));
224     principal_data2.realm.data = "ZEUS.MIT.EDU";
225     principal_data2.realm.length = strlen(principal_data2.realm.data);
226     creds.server = &principal_data2;
227     memcpy(&creds.keyblock, &key.keyblock, sizeof(creds.keyblock));
228     creds.times.authtime = 0;
229     creds.times.starttime = 1;
230     creds.times.endtime = 2;
231     creds.times.renew_till = 3;
232     creds.is_skey = FALSE;
233     creds.ticket_flags = 0;
234     creds.addresses = NULL;
235     creds.ticket.magic = 0;
236     creds.ticket.length = strlen(str);
237     creds.ticket.data = str;
238     creds.second_ticket.magic = 0;
239     creds.second_ticket.length = strlen(str);
240     creds.second_ticket.data = str;
241     creds.authdata = NULL;
242     TRACE(ctx, "krb5_creds *, display clientprinc -> serverprinc: {creds}",
243           &creds);
244
245     krb5_free_context(ctx);
246     return 0;
247 }