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