]> The Tcpdump Group git mirrors - tcpdump/blob - print-krb.c
initial checkin
[tcpdump] / print-krb.c
1 /*
2 * Copyright (c) 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Initial contribution from John Hawkinson (jhawk@mit.edu).
22 */
23
24 #ifndef lint
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-krb.c,v 1.18 2002-09-05 21:25:43 guy Exp $";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36
37 #include "interface.h"
38 #include "addrtoname.h"
39
40 static const u_char *c_print(register const u_char *, register const u_char *);
41 static const u_char *krb4_print_hdr(const u_char *);
42 static void krb4_print(const u_char *);
43
44 #define AUTH_MSG_KDC_REQUEST 1<<1
45 #define AUTH_MSG_KDC_REPLY 2<<1
46 #define AUTH_MSG_APPL_REQUEST 3<<1
47 #define AUTH_MSG_APPL_REQUEST_MUTUAL 4<<1
48 #define AUTH_MSG_ERR_REPLY 5<<1
49 #define AUTH_MSG_PRIVATE 6<<1
50 #define AUTH_MSG_SAFE 7<<1
51 #define AUTH_MSG_APPL_ERR 8<<1
52 #define AUTH_MSG_DIE 63<<1
53
54 #define KERB_ERR_OK 0
55 #define KERB_ERR_NAME_EXP 1
56 #define KERB_ERR_SERVICE_EXP 2
57 #define KERB_ERR_AUTH_EXP 3
58 #define KERB_ERR_PKT_VER 4
59 #define KERB_ERR_NAME_MAST_KEY_VER 5
60 #define KERB_ERR_SERV_MAST_KEY_VER 6
61 #define KERB_ERR_BYTE_ORDER 7
62 #define KERB_ERR_PRINCIPAL_UNKNOWN 8
63 #define KERB_ERR_PRINCIPAL_NOT_UNIQUE 9
64 #define KERB_ERR_NULL_KEY 10
65
66 struct krb {
67 u_char pvno; /* Protocol Version */
68 u_char type; /* Type+B */
69 };
70
71 static char tstr[] = " [|kerberos]";
72
73 static struct tok type2str[] = {
74 { AUTH_MSG_KDC_REQUEST, "KDC_REQUEST" },
75 { AUTH_MSG_KDC_REPLY, "KDC_REPLY" },
76 { AUTH_MSG_APPL_REQUEST, "APPL_REQUEST" },
77 { AUTH_MSG_APPL_REQUEST_MUTUAL, "APPL_REQUEST_MUTUAL" },
78 { AUTH_MSG_ERR_REPLY, "ERR_REPLY" },
79 { AUTH_MSG_PRIVATE, "PRIVATE" },
80 { AUTH_MSG_SAFE, "SAFE" },
81 { AUTH_MSG_APPL_ERR, "APPL_ERR" },
82 { AUTH_MSG_DIE, "DIE" },
83 { 0, NULL }
84 };
85
86 static struct tok kerr2str[] = {
87 { KERB_ERR_OK, "OK" },
88 { KERB_ERR_NAME_EXP, "NAME_EXP" },
89 { KERB_ERR_SERVICE_EXP, "SERVICE_EXP" },
90 { KERB_ERR_AUTH_EXP, "AUTH_EXP" },
91 { KERB_ERR_PKT_VER, "PKT_VER" },
92 { KERB_ERR_NAME_MAST_KEY_VER, "NAME_MAST_KEY_VER" },
93 { KERB_ERR_SERV_MAST_KEY_VER, "SERV_MAST_KEY_VER" },
94 { KERB_ERR_BYTE_ORDER, "BYTE_ORDER" },
95 { KERB_ERR_PRINCIPAL_UNKNOWN, "PRINCIPAL_UNKNOWN" },
96 { KERB_ERR_PRINCIPAL_NOT_UNIQUE,"PRINCIPAL_NOT_UNIQUE" },
97 { KERB_ERR_NULL_KEY, "NULL_KEY"},
98 { 0, NULL}
99 };
100
101
102 /* little endian (unaligned) to host byte order */
103 /* XXX need to look at this... */
104 #define vtohlp(x) ((( ((char *)(x))[0] ) ) | \
105 (( ((char *)(x))[1] ) << 8) | \
106 (( ((char *)(x))[2] ) << 16) | \
107 (( ((char *)(x))[3] ) << 24))
108 #define vtohsp(x) ((( ((char *)(x))[0] ) ) | \
109 (( ((char *)(x))[1] ) << 8))
110 /* network (big endian) (unaligned) to host byte order */
111 #define ntohlp(x) ((( ((char *)(x))[3] ) ) | \
112 (( ((char *)(x))[2] ) << 8) | \
113 (( ((char *)(x))[1] ) << 16) | \
114 (( ((char *)(x))[0] ) << 24))
115 #define ntohsp(x) ((( ((char *)(x))[1] ) ) | \
116 (( ((char *)(x))[0] ) << 8))
117
118
119
120 static const u_char *
121 c_print(register const u_char *s, register const u_char *ep)
122 {
123 register u_char c;
124 register int flag;
125
126 flag = 1;
127 while (s < ep) {
128 c = *s++;
129 if (c == '\0') {
130 flag = 0;
131 break;
132 }
133 if (!isascii(c)) {
134 c = toascii(c);
135 putchar('M');
136 putchar('-');
137 }
138 if (!isprint(c)) {
139 c ^= 0x40; /* DEL to ?, others to alpha */
140 putchar('^');
141 }
142 putchar(c);
143 }
144 if (flag)
145 return NULL;
146 return (s);
147 }
148
149 static const u_char *
150 krb4_print_hdr(const u_char *cp)
151 {
152 cp += 2;
153
154 #define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc
155
156 PRINT;
157 putchar('.');
158 PRINT;
159 putchar('@');
160 PRINT;
161 return (cp);
162
163 trunc:
164 fputs(tstr, stdout);
165 return (NULL);
166
167 #undef PRINT
168 }
169
170 static void
171 krb4_print(const u_char *cp)
172 {
173 register const struct krb *kp;
174 u_char type;
175 u_short len;
176
177 #define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc
178 /* True if struct krb is little endian */
179 #define IS_LENDIAN(kp) (((kp)->type & 0x01) != 0)
180 #define KTOHSP(kp, cp) (IS_LENDIAN(kp) ? vtohsp(cp) : ntohsp(cp))
181
182 kp = (struct krb *)cp;
183
184 if ((&kp->type) >= snapend) {
185 fputs(tstr, stdout);
186 return;
187 }
188
189 type = kp->type & (0xFF << 1);
190
191 printf(" %s %s: ",
192 IS_LENDIAN(kp) ? "le" : "be", tok2str(type2str, NULL, type));
193
194 switch (type) {
195
196 case AUTH_MSG_KDC_REQUEST:
197 if ((cp = krb4_print_hdr(cp)) == NULL)
198 return;
199 cp += 4; /* ctime */
200 TCHECK(*cp);
201 printf(" %dmin ", *cp++ * 5);
202 PRINT;
203 putchar('.');
204 PRINT;
205 break;
206
207 case AUTH_MSG_APPL_REQUEST:
208 cp += 2;
209 TCHECK(*cp);
210 printf("v%d ", *cp++);
211 PRINT;
212 TCHECK(*cp);
213 printf(" (%d)", *cp++);
214 TCHECK(*cp);
215 printf(" (%d)", *cp);
216 break;
217
218 case AUTH_MSG_KDC_REPLY:
219 if ((cp = krb4_print_hdr(cp)) == NULL)
220 return;
221 cp += 10; /* timestamp + n + exp + kvno */
222 TCHECK2(*cp, sizeof(short));
223 len = KTOHSP(kp, cp);
224 printf(" (%d)", len);
225 break;
226
227 case AUTH_MSG_ERR_REPLY:
228 if ((cp = krb4_print_hdr(cp)) == NULL)
229 return;
230 cp += 4; /* timestamp */
231 TCHECK2(*cp, sizeof(short));
232 printf(" %s ", tok2str(kerr2str, NULL, KTOHSP(kp, cp)));
233 cp += 4;
234 PRINT;
235 break;
236
237 default:
238 fputs("(unknown)", stdout);
239 break;
240 }
241
242 return;
243 trunc:
244 fputs(tstr, stdout);
245 }
246
247 void
248 krb_print(const u_char *dat)
249 {
250 register const struct krb *kp;
251
252 kp = (struct krb *)dat;
253
254 if (dat >= snapend) {
255 fputs(tstr, stdout);
256 return;
257 }
258
259 switch (kp->pvno) {
260
261 case 1:
262 case 2:
263 case 3:
264 printf(" v%d", kp->pvno);
265 break;
266
267 case 4:
268 printf(" v%d", kp->pvno);
269 krb4_print((const u_char *)kp);
270 break;
271
272 case 106:
273 case 107:
274 fputs(" v5", stdout);
275 /* Decode ASN.1 here "someday" */
276 break;
277 }
278 return;
279 }