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