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