]> The Tcpdump Group git mirrors - tcpdump/blob - print-krb.c
1b70cb06267f10339e3493d51e4af13b31a13c79
[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.16 2002-08-01 08:53:14 risso Exp $";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <errno.h>
36 #include <stdio.h>
37
38 #include "interface.h"
39 #include "addrtoname.h"
40
41 const u_char *c_print(register const u_char *, register const u_char *);
42 const u_char *krb4_print_hdr(const u_char *);
43 void krb4_print(const u_char *);
44 void krb_print(const u_char *, u_int);
45
46
47 #define AUTH_MSG_KDC_REQUEST 1<<1
48 #define AUTH_MSG_KDC_REPLY 2<<1
49 #define AUTH_MSG_APPL_REQUEST 3<<1
50 #define AUTH_MSG_APPL_REQUEST_MUTUAL 4<<1
51 #define AUTH_MSG_ERR_REPLY 5<<1
52 #define AUTH_MSG_PRIVATE 6<<1
53 #define AUTH_MSG_SAFE 7<<1
54 #define AUTH_MSG_APPL_ERR 8<<1
55 #define AUTH_MSG_DIE 63<<1
56
57 #define KERB_ERR_OK 0
58 #define KERB_ERR_NAME_EXP 1
59 #define KERB_ERR_SERVICE_EXP 2
60 #define KERB_ERR_AUTH_EXP 3
61 #define KERB_ERR_PKT_VER 4
62 #define KERB_ERR_NAME_MAST_KEY_VER 5
63 #define KERB_ERR_SERV_MAST_KEY_VER 6
64 #define KERB_ERR_BYTE_ORDER 7
65 #define KERB_ERR_PRINCIPAL_UNKNOWN 8
66 #define KERB_ERR_PRINCIPAL_NOT_UNIQUE 9
67 #define KERB_ERR_NULL_KEY 10
68
69 struct krb {
70 u_char pvno; /* Protocol Version */
71 u_char type; /* Type+B */
72 };
73
74 static char tstr[] = " [|kerberos]";
75
76 static struct tok type2str[] = {
77 { AUTH_MSG_KDC_REQUEST, "KDC_REQUEST" },
78 { AUTH_MSG_KDC_REPLY, "KDC_REPLY" },
79 { AUTH_MSG_APPL_REQUEST, "APPL_REQUEST" },
80 { AUTH_MSG_APPL_REQUEST_MUTUAL, "APPL_REQUEST_MUTUAL" },
81 { AUTH_MSG_ERR_REPLY, "ERR_REPLY" },
82 { AUTH_MSG_PRIVATE, "PRIVATE" },
83 { AUTH_MSG_SAFE, "SAFE" },
84 { AUTH_MSG_APPL_ERR, "APPL_ERR" },
85 { AUTH_MSG_DIE, "DIE" },
86 { 0, NULL }
87 };
88
89 static struct tok kerr2str[] = {
90 { KERB_ERR_OK, "OK" },
91 { KERB_ERR_NAME_EXP, "NAME_EXP" },
92 { KERB_ERR_SERVICE_EXP, "SERVICE_EXP" },
93 { KERB_ERR_AUTH_EXP, "AUTH_EXP" },
94 { KERB_ERR_PKT_VER, "PKT_VER" },
95 { KERB_ERR_NAME_MAST_KEY_VER, "NAME_MAST_KEY_VER" },
96 { KERB_ERR_SERV_MAST_KEY_VER, "SERV_MAST_KEY_VER" },
97 { KERB_ERR_BYTE_ORDER, "BYTE_ORDER" },
98 { KERB_ERR_PRINCIPAL_UNKNOWN, "PRINCIPAL_UNKNOWN" },
99 { KERB_ERR_PRINCIPAL_NOT_UNIQUE,"PRINCIPAL_NOT_UNIQUE" },
100 { KERB_ERR_NULL_KEY, "NULL_KEY"},
101 { 0, NULL}
102 };
103
104
105 /* little endian (unaligned) to host byte order */
106 /* XXX need to look at this... */
107 #define vtohlp(x) ((( ((char *)(x))[0] ) ) | \
108 (( ((char *)(x))[1] ) << 8) | \
109 (( ((char *)(x))[2] ) << 16) | \
110 (( ((char *)(x))[3] ) << 24))
111 #define vtohsp(x) ((( ((char *)(x))[0] ) ) | \
112 (( ((char *)(x))[1] ) << 8))
113 /* network (big endian) (unaligned) to host byte order */
114 #define ntohlp(x) ((( ((char *)(x))[3] ) ) | \
115 (( ((char *)(x))[2] ) << 8) | \
116 (( ((char *)(x))[1] ) << 16) | \
117 (( ((char *)(x))[0] ) << 24))
118 #define ntohsp(x) ((( ((char *)(x))[1] ) ) | \
119 (( ((char *)(x))[0] ) << 8))
120
121
122
123 const u_char *
124 c_print(register const u_char *s, register const u_char *ep)
125 {
126 register u_char c;
127 register int flag;
128
129 flag = 1;
130 while (s < ep) {
131 c = *s++;
132 if (c == '\0') {
133 flag = 0;
134 break;
135 }
136 if (!isascii(c)) {
137 c = toascii(c);
138 putchar('M');
139 putchar('-');
140 }
141 if (!isprint(c)) {
142 c ^= 0x40; /* DEL to ?, others to alpha */
143 putchar('^');
144 }
145 putchar(c);
146 }
147 if (flag)
148 return NULL;
149 return (s);
150 }
151
152 const u_char *
153 krb4_print_hdr(const u_char *cp)
154 {
155 cp += 2;
156
157 #define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc
158
159 PRINT;
160 putchar('.');
161 PRINT;
162 putchar('@');
163 PRINT;
164 return (cp);
165
166 trunc:
167 fputs(tstr, stdout);
168 return (NULL);
169
170 #undef PRINT
171 }
172
173 void
174 krb4_print(const u_char *cp)
175 {
176 register const struct krb *kp;
177 u_char type;
178 u_short len;
179
180 #define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc
181 /* True if struct krb is little endian */
182 #define IS_LENDIAN(kp) (((kp)->type & 0x01) != 0)
183 #define KTOHSP(kp, cp) (IS_LENDIAN(kp) ? vtohsp(cp) : ntohsp(cp))
184
185 kp = (struct krb *)cp;
186
187 if ((&kp->type) >= snapend) {
188 fputs(tstr, stdout);
189 return;
190 }
191
192 type = kp->type & (0xFF << 1);
193
194 printf(" %s %s: ",
195 IS_LENDIAN(kp) ? "le" : "be", tok2str(type2str, NULL, type));
196
197 switch (type) {
198
199 case AUTH_MSG_KDC_REQUEST:
200 if ((cp = krb4_print_hdr(cp)) == NULL)
201 return;
202 cp += 4; /* ctime */
203 TCHECK(*cp);
204 printf(" %dmin ", *cp++ * 5);
205 PRINT;
206 putchar('.');
207 PRINT;
208 break;
209
210 case AUTH_MSG_APPL_REQUEST:
211 cp += 2;
212 TCHECK(*cp);
213 printf("v%d ", *cp++);
214 PRINT;
215 TCHECK(*cp);
216 printf(" (%d)", *cp++);
217 TCHECK(*cp);
218 printf(" (%d)", *cp);
219 break;
220
221 case AUTH_MSG_KDC_REPLY:
222 if ((cp = krb4_print_hdr(cp)) == NULL)
223 return;
224 cp += 10; /* timestamp + n + exp + kvno */
225 TCHECK2(*cp, sizeof(short));
226 len = KTOHSP(kp, cp);
227 printf(" (%d)", len);
228 break;
229
230 case AUTH_MSG_ERR_REPLY:
231 if ((cp = krb4_print_hdr(cp)) == NULL)
232 return;
233 cp += 4; /* timestamp */
234 TCHECK2(*cp, sizeof(short));
235 printf(" %s ", tok2str(kerr2str, NULL, KTOHSP(kp, cp)));
236 cp += 4;
237 PRINT;
238 break;
239
240 default:
241 fputs("(unknown)", stdout);
242 break;
243 }
244
245 return;
246 trunc:
247 fputs(tstr, stdout);
248 }
249
250 void
251 krb_print(const u_char *dat, u_int length)
252 {
253 register const struct krb *kp;
254
255 kp = (struct krb *)dat;
256
257 if (dat >= snapend) {
258 fputs(tstr, stdout);
259 return;
260 }
261
262 switch (kp->pvno) {
263
264 case 1:
265 case 2:
266 case 3:
267 printf(" v%d", kp->pvno);
268 break;
269
270 case 4:
271 printf(" v%d", kp->pvno);
272 krb4_print((const u_char *)kp);
273 break;
274
275 case 106:
276 case 107:
277 fputs(" v5", stdout);
278 /* Decode ASN.1 here "someday" */
279 break;
280 }
281 return;
282 }