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