print-krb.c revision 75115
1243730Srwatson/* 2243730Srwatson * Copyright (c) 1995, 1996, 1997 3243730Srwatson * The Regents of the University of California. All rights reserved. 4243730Srwatson * 5243730Srwatson * Redistribution and use in source and binary forms, with or without 6243730Srwatson * modification, are permitted provided that: (1) source code distributions 7243730Srwatson * retain the above copyright notice and this paragraph in its entirety, (2) 8243730Srwatson * distributions including binary code include the above copyright notice and 9243730Srwatson * this paragraph in its entirety in the documentation or other materials 10243730Srwatson * provided with the distribution, and (3) all advertising materials mentioning 11243730Srwatson * features or use of this software display the following acknowledgement: 12243730Srwatson * ``This product includes software developed by the University of California, 13243730Srwatson * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14243730Srwatson * the University nor the names of its contributors may be used to endorse 15243730Srwatson * or promote products derived from this software without specific prior 16243730Srwatson * written permission. 17243730Srwatson * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18243730Srwatson * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19243730Srwatson * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20243730Srwatson * 21243730Srwatson * Initial contribution from John Hawkinson (jhawk@mit.edu). 22243730Srwatson */ 23243730Srwatson 24243730Srwatson#ifndef lint 25243730Srwatsonstatic const char rcsid[] = 26243730Srwatson "@(#) $Header: /tcpdump/master/tcpdump/print-krb.c,v 1.15 2000/09/29 04:58:42 guy Exp $"; 27243730Srwatson#endif 28243730Srwatson 29243730Srwatson#ifdef HAVE_CONFIG_H 30243730Srwatson#include "config.h" 31243730Srwatson#endif 32243730Srwatson 33243730Srwatson#include <sys/param.h> 34243730Srwatson#include <sys/time.h> 35243730Srwatson#include <sys/socket.h> 36243730Srwatson 37243730Srwatson#include <netinet/in.h> 38243730Srwatson 39243730Srwatson#include <ctype.h> 40243730Srwatson#include <errno.h> 41243730Srwatson#include <stdio.h> 42243730Srwatson 43243730Srwatson#include "interface.h" 44243730Srwatson#include "addrtoname.h" 45243730Srwatson 46243730Srwatsonconst u_char *c_print(register const u_char *, register const u_char *); 47243730Srwatsonconst u_char *krb4_print_hdr(const u_char *); 48243730Srwatsonvoid krb4_print(const u_char *); 49243730Srwatsonvoid krb_print(const u_char *, u_int); 50243730Srwatson 51243730Srwatson 52243730Srwatson#define AUTH_MSG_KDC_REQUEST 1<<1 53243730Srwatson#define AUTH_MSG_KDC_REPLY 2<<1 54243730Srwatson#define AUTH_MSG_APPL_REQUEST 3<<1 55243730Srwatson#define AUTH_MSG_APPL_REQUEST_MUTUAL 4<<1 56243730Srwatson#define AUTH_MSG_ERR_REPLY 5<<1 57243730Srwatson#define AUTH_MSG_PRIVATE 6<<1 58#define AUTH_MSG_SAFE 7<<1 59#define AUTH_MSG_APPL_ERR 8<<1 60#define AUTH_MSG_DIE 63<<1 61 62#define KERB_ERR_OK 0 63#define KERB_ERR_NAME_EXP 1 64#define KERB_ERR_SERVICE_EXP 2 65#define KERB_ERR_AUTH_EXP 3 66#define KERB_ERR_PKT_VER 4 67#define KERB_ERR_NAME_MAST_KEY_VER 5 68#define KERB_ERR_SERV_MAST_KEY_VER 6 69#define KERB_ERR_BYTE_ORDER 7 70#define KERB_ERR_PRINCIPAL_UNKNOWN 8 71#define KERB_ERR_PRINCIPAL_NOT_UNIQUE 9 72#define KERB_ERR_NULL_KEY 10 73 74struct krb { 75 u_char pvno; /* Protocol Version */ 76 u_char type; /* Type+B */ 77}; 78 79static char tstr[] = " [|kerberos]"; 80 81static struct tok type2str[] = { 82 { AUTH_MSG_KDC_REQUEST, "KDC_REQUEST" }, 83 { AUTH_MSG_KDC_REPLY, "KDC_REPLY" }, 84 { AUTH_MSG_APPL_REQUEST, "APPL_REQUEST" }, 85 { AUTH_MSG_APPL_REQUEST_MUTUAL, "APPL_REQUEST_MUTUAL" }, 86 { AUTH_MSG_ERR_REPLY, "ERR_REPLY" }, 87 { AUTH_MSG_PRIVATE, "PRIVATE" }, 88 { AUTH_MSG_SAFE, "SAFE" }, 89 { AUTH_MSG_APPL_ERR, "APPL_ERR" }, 90 { AUTH_MSG_DIE, "DIE" }, 91 { 0, NULL } 92}; 93 94static struct tok kerr2str[] = { 95 { KERB_ERR_OK, "OK" }, 96 { KERB_ERR_NAME_EXP, "NAME_EXP" }, 97 { KERB_ERR_SERVICE_EXP, "SERVICE_EXP" }, 98 { KERB_ERR_AUTH_EXP, "AUTH_EXP" }, 99 { KERB_ERR_PKT_VER, "PKT_VER" }, 100 { KERB_ERR_NAME_MAST_KEY_VER, "NAME_MAST_KEY_VER" }, 101 { KERB_ERR_SERV_MAST_KEY_VER, "SERV_MAST_KEY_VER" }, 102 { KERB_ERR_BYTE_ORDER, "BYTE_ORDER" }, 103 { KERB_ERR_PRINCIPAL_UNKNOWN, "PRINCIPAL_UNKNOWN" }, 104 { KERB_ERR_PRINCIPAL_NOT_UNIQUE,"PRINCIPAL_NOT_UNIQUE" }, 105 { KERB_ERR_NULL_KEY, "NULL_KEY"}, 106 { 0, NULL} 107}; 108 109 110/* little endian (unaligned) to host byte order */ 111/* XXX need to look at this... */ 112#define vtohlp(x) ((( ((char *)(x))[0] ) ) | \ 113 (( ((char *)(x))[1] ) << 8) | \ 114 (( ((char *)(x))[2] ) << 16) | \ 115 (( ((char *)(x))[3] ) << 24)) 116#define vtohsp(x) ((( ((char *)(x))[0] ) ) | \ 117 (( ((char *)(x))[1] ) << 8)) 118/* network (big endian) (unaligned) to host byte order */ 119#define ntohlp(x) ((( ((char *)(x))[3] ) ) | \ 120 (( ((char *)(x))[2] ) << 8) | \ 121 (( ((char *)(x))[1] ) << 16) | \ 122 (( ((char *)(x))[0] ) << 24)) 123#define ntohsp(x) ((( ((char *)(x))[1] ) ) | \ 124 (( ((char *)(x))[0] ) << 8)) 125 126 127 128const u_char * 129c_print(register const u_char *s, register const u_char *ep) 130{ 131 register u_char c; 132 register int flag; 133 134 flag = 1; 135 while (s < ep) { 136 c = *s++; 137 if (c == '\0') { 138 flag = 0; 139 break; 140 } 141 if (!isascii(c)) { 142 c = toascii(c); 143 putchar('M'); 144 putchar('-'); 145 } 146 if (!isprint(c)) { 147 c ^= 0x40; /* DEL to ?, others to alpha */ 148 putchar('^'); 149 } 150 putchar(c); 151 } 152 if (flag) 153 return NULL; 154 return (s); 155} 156 157const u_char * 158krb4_print_hdr(const u_char *cp) 159{ 160 cp += 2; 161 162#define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc 163 164 PRINT; 165 putchar('.'); 166 PRINT; 167 putchar('@'); 168 PRINT; 169 return (cp); 170 171trunc: 172 fputs(tstr, stdout); 173 return (NULL); 174 175#undef PRINT 176} 177 178void 179krb4_print(const u_char *cp) 180{ 181 register const struct krb *kp; 182 u_char type; 183 u_short len; 184 185#define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc 186/* True if struct krb is little endian */ 187#define IS_LENDIAN(kp) (((kp)->type & 0x01) != 0) 188#define KTOHSP(kp, cp) (IS_LENDIAN(kp) ? vtohsp(cp) : ntohsp(cp)) 189 190 kp = (struct krb *)cp; 191 192 if ((&kp->type) >= snapend) { 193 fputs(tstr, stdout); 194 return; 195 } 196 197 type = kp->type & (0xFF << 1); 198 199 printf(" %s %s: ", 200 IS_LENDIAN(kp) ? "le" : "be", tok2str(type2str, NULL, type)); 201 202 switch (type) { 203 204 case AUTH_MSG_KDC_REQUEST: 205 if ((cp = krb4_print_hdr(cp)) == NULL) 206 return; 207 cp += 4; /* ctime */ 208 TCHECK(*cp); 209 printf(" %dmin ", *cp++ * 5); 210 PRINT; 211 putchar('.'); 212 PRINT; 213 break; 214 215 case AUTH_MSG_APPL_REQUEST: 216 cp += 2; 217 TCHECK(*cp); 218 printf("v%d ", *cp++); 219 PRINT; 220 TCHECK(*cp); 221 printf(" (%d)", *cp++); 222 TCHECK(*cp); 223 printf(" (%d)", *cp); 224 break; 225 226 case AUTH_MSG_KDC_REPLY: 227 if ((cp = krb4_print_hdr(cp)) == NULL) 228 return; 229 cp += 10; /* timestamp + n + exp + kvno */ 230 TCHECK2(*cp, sizeof(short)); 231 len = KTOHSP(kp, cp); 232 printf(" (%d)", len); 233 break; 234 235 case AUTH_MSG_ERR_REPLY: 236 if ((cp = krb4_print_hdr(cp)) == NULL) 237 return; 238 cp += 4; /* timestamp */ 239 TCHECK2(*cp, sizeof(short)); 240 printf(" %s ", tok2str(kerr2str, NULL, KTOHSP(kp, cp))); 241 cp += 4; 242 PRINT; 243 break; 244 245 default: 246 fputs("(unknown)", stdout); 247 break; 248 } 249 250 return; 251trunc: 252 fputs(tstr, stdout); 253} 254 255void 256krb_print(const u_char *dat, u_int length) 257{ 258 register const struct krb *kp; 259 260 kp = (struct krb *)dat; 261 262 if (dat >= snapend) { 263 fputs(tstr, stdout); 264 return; 265 } 266 267 switch (kp->pvno) { 268 269 case 1: 270 case 2: 271 case 3: 272 printf(" v%d", kp->pvno); 273 break; 274 275 case 4: 276 printf(" v%d", kp->pvno); 277 krb4_print((const u_char *)kp); 278 break; 279 280 case 106: 281 case 107: 282 fputs(" v5", stdout); 283 /* Decode ASN.1 here "someday" */ 284 break; 285 } 286 return; 287} 288