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