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