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