1 /* $OpenBSD: print-krb.c,v 1.8 2009/10/27 23:59:55 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1995, 1996, 1997 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * 23 * Initial contribution from John Hawkinson (jhawk@mit.edu). 24 */ 25 26 #include <sys/param.h> 27 #include <sys/time.h> 28 #include <sys/socket.h> 29 30 #include <netinet/in.h> 31 #include <netinet/in_systm.h> 32 #include <netinet/ip.h> 33 #include <netinet/ip_var.h> 34 #include <netinet/udp.h> 35 #include <netinet/udp_var.h> 36 37 #include <ctype.h> 38 #include <errno.h> 39 #include <stdio.h> 40 41 #include "interface.h" 42 #include "addrtoname.h" 43 44 const u_char *c_print(register const u_char *, register const u_char *); 45 const u_char *krb4_print_hdr(const u_char *); 46 void krb4_print(const u_char *); 47 void krb_print(const u_char *, u_int); 48 49 50 #define AUTH_MSG_KDC_REQUEST 1<<1 51 #define AUTH_MSG_KDC_REPLY 2<<1 52 #define AUTH_MSG_APPL_REQUEST 3<<1 53 #define AUTH_MSG_APPL_REQUEST_MUTUAL 4<<1 54 #define AUTH_MSG_ERR_REPLY 5<<1 55 #define AUTH_MSG_PRIVATE 6<<1 56 #define AUTH_MSG_SAFE 7<<1 57 #define AUTH_MSG_APPL_ERR 8<<1 58 #define AUTH_MSG_DIE 63<<1 59 60 #define KERB_ERR_OK 0 61 #define KERB_ERR_NAME_EXP 1 62 #define KERB_ERR_SERVICE_EXP 2 63 #define KERB_ERR_AUTH_EXP 3 64 #define KERB_ERR_PKT_VER 4 65 #define KERB_ERR_NAME_MAST_KEY_VER 5 66 #define KERB_ERR_SERV_MAST_KEY_VER 6 67 #define KERB_ERR_BYTE_ORDER 7 68 #define KERB_ERR_PRINCIPAL_UNKNOWN 8 69 #define KERB_ERR_PRINCIPAL_NOT_UNIQUE 9 70 #define KERB_ERR_NULL_KEY 10 71 72 struct krb { 73 u_char pvno; /* Protocol Version */ 74 u_char type; /* Type+B */ 75 }; 76 77 static char tstr[] = " [|kerberos]"; 78 79 static struct tok type2str[] = { 80 { AUTH_MSG_KDC_REQUEST, "KDC_REQUEST" }, 81 { AUTH_MSG_KDC_REPLY, "KDC_REPLY" }, 82 { AUTH_MSG_APPL_REQUEST, "APPL_REQUEST" }, 83 { AUTH_MSG_APPL_REQUEST_MUTUAL, "APPL_REQUEST_MUTUAL" }, 84 { AUTH_MSG_ERR_REPLY, "ERR_REPLY" }, 85 { AUTH_MSG_PRIVATE, "PRIVATE" }, 86 { AUTH_MSG_SAFE, "SAFE" }, 87 { AUTH_MSG_APPL_ERR, "APPL_ERR" }, 88 { AUTH_MSG_DIE, "DIE" }, 89 { 0, NULL } 90 }; 91 92 static struct tok kerr2str[] = { 93 { KERB_ERR_OK, "OK" }, 94 { KERB_ERR_NAME_EXP, "NAME_EXP" }, 95 { KERB_ERR_SERVICE_EXP, "SERVICE_EXP" }, 96 { KERB_ERR_AUTH_EXP, "AUTH_EXP" }, 97 { KERB_ERR_PKT_VER, "PKT_VER" }, 98 { KERB_ERR_NAME_MAST_KEY_VER, "NAME_MAST_KEY_VER" }, 99 { KERB_ERR_SERV_MAST_KEY_VER, "SERV_MAST_KEY_VER" }, 100 { KERB_ERR_BYTE_ORDER, "BYTE_ORDER" }, 101 { KERB_ERR_PRINCIPAL_UNKNOWN, "PRINCIPAL_UNKNOWN" }, 102 { KERB_ERR_PRINCIPAL_NOT_UNIQUE,"PRINCIPAL_NOT_UNIQUE" }, 103 { KERB_ERR_NULL_KEY, "NULL_KEY"}, 104 { 0, NULL} 105 }; 106 107 108 /* little endian (unaligned) to host byte order */ 109 /* XXX need to look at this... */ 110 #define vtohlp(x) ((( ((char *)(x))[0] ) ) | \ 111 (( ((char *)(x))[1] ) << 8) | \ 112 (( ((char *)(x))[2] ) << 16) | \ 113 (( ((char *)(x))[3] ) << 24)) 114 #define vtohsp(x) ((( ((char *)(x))[0] ) ) | \ 115 (( ((char *)(x))[1] ) << 8)) 116 /* network (big endian) (unaligned) to host byte order */ 117 #define ntohlp(x) ((( ((char *)(x))[3] ) ) | \ 118 (( ((char *)(x))[2] ) << 8) | \ 119 (( ((char *)(x))[1] ) << 16) | \ 120 (( ((char *)(x))[0] ) << 24)) 121 #define ntohsp(x) ((( ((char *)(x))[1] ) ) | \ 122 (( ((char *)(x))[0] ) << 8)) 123 124 125 126 const u_char * 127 c_print(register const u_char *s, register const u_char *ep) 128 { 129 register u_char c; 130 register int flag; 131 132 flag = 1; 133 while (ep == NULL || s < ep) { 134 c = *s++; 135 if (c == '\0') { 136 flag = 0; 137 break; 138 } 139 if (!isascii(c)) { 140 c = toascii(c); 141 putchar('M'); 142 putchar('-'); 143 } 144 if (!isprint(c)) { 145 c ^= 0x40; /* DEL to ?, others to alpha */ 146 putchar('^'); 147 } 148 putchar(c); 149 } 150 if (flag) 151 return NULL; 152 return (s); 153 } 154 155 const u_char * 156 krb4_print_hdr(const u_char *cp) 157 { 158 cp += 2; 159 160 #define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc 161 162 TCHECK2(cp, 0); 163 PRINT; 164 TCHECK2(cp, 0); 165 putchar('.'); PRINT; 166 TCHECK2(cp, 0); 167 putchar('@'); PRINT; 168 return(cp); 169 170 trunc: 171 fputs(tstr, stdout); 172 return (NULL); 173 174 #undef PRINT 175 } 176 177 void 178 krb4_print(const u_char *cp) 179 { 180 register const struct krb *kp; 181 u_char type; 182 u_short len; 183 184 #define PRINT if ((cp = c_print(cp, snapend)) == NULL) goto trunc 185 /* True if struct krb is little endian */ 186 #define IS_LENDIAN(kp) (((kp)->type & 0x01) != 0) 187 #define KTOHSP(kp, cp) (IS_LENDIAN(kp) ? vtohsp(cp) : ntohsp(cp)) 188 189 kp = (struct krb *)cp; 190 191 if ((&kp->type) >= snapend) { 192 fputs(tstr, stdout); 193 return; 194 } 195 196 type = kp->type & (0xFF << 1); 197 198 printf(" %s %s: ", 199 IS_LENDIAN(kp) ? "le" : "be", tok2str(type2str, NULL, type)); 200 201 switch (type) { 202 203 case AUTH_MSG_KDC_REQUEST: 204 if ((cp = krb4_print_hdr(cp)) == NULL) 205 return; 206 cp += 4; /* ctime */ 207 TCHECK2(cp, 0); 208 printf(" %dmin ", *cp++ * 5); 209 TCHECK2(cp, 0); 210 PRINT; 211 TCHECK2(cp, 0); 212 putchar('.'); PRINT; 213 break; 214 215 case AUTH_MSG_APPL_REQUEST: 216 cp += 2; 217 TCHECK2(cp, 0); 218 printf("v%d ", *cp++); 219 TCHECK2(cp, 0); 220 PRINT; 221 TCHECK2(cp, 0); 222 printf(" (%d)", *cp++); 223 TCHECK2(cp, 0); 224 printf(" (%d)", *cp); 225 TCHECK2(cp, 0); 226 break; 227 228 case AUTH_MSG_KDC_REPLY: 229 if ((cp = krb4_print_hdr(cp)) == NULL) 230 return; 231 cp += 10; /* timestamp + n + exp + kvno */ 232 TCHECK2(cp, 0); 233 len = KTOHSP(kp, cp); 234 printf(" (%d)", len); 235 TCHECK2(cp, 0); 236 break; 237 238 case AUTH_MSG_ERR_REPLY: 239 if ((cp = krb4_print_hdr(cp)) == NULL) 240 return; 241 cp += 4; /* timestamp */ 242 TCHECK2(cp, 0); 243 printf(" %s ", tok2str(kerr2str, NULL, KTOHSP(kp, cp))); 244 cp += 4; 245 TCHECK2(cp, 0); 246 PRINT; 247 break; 248 249 default: 250 fputs("(unknown)", stdout); 251 break; 252 } 253 254 return; 255 trunc: 256 fputs(tstr, stdout); 257 } 258 259 void 260 krb_print(const u_char *dat, u_int length) 261 { 262 register const struct krb *kp; 263 264 kp = (struct krb *)dat; 265 266 if (dat >= snapend) { 267 fputs(tstr, stdout); 268 return; 269 } 270 271 switch (kp->pvno) { 272 273 case 1: 274 case 2: 275 case 3: 276 printf(" v%d", kp->pvno); 277 break; 278 279 case 4: 280 printf(" v%d", kp->pvno); 281 krb4_print((const u_char *)kp); 282 break; 283 284 case 106: 285 case 107: 286 fputs(" v5", stdout); 287 /* Decode ASN.1 here "someday" */ 288 break; 289 } 290 return; 291 } 292