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